Home | History | Annotate | Line # | Download | only in uvm
uvm_aobj.c revision 1.26
      1 /*	$NetBSD: uvm_aobj.c,v 1.26 1999/09/12 01:17:34 chs Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998 Chuck Silvers, Charles D. Cranor and
      5  *                    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  * from: Id: uvm_aobj.c,v 1.1.2.5 1998/02/06 05:14:38 chs Exp
     35  */
     36 /*
     37  * uvm_aobj.c: anonymous memory uvm_object pager
     38  *
     39  * author: Chuck Silvers <chuq (at) chuq.com>
     40  * started: Jan-1998
     41  *
     42  * - design mostly from Chuck Cranor
     43  */
     44 
     45 
     46 
     47 #include "opt_uvmhist.h"
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/proc.h>
     52 #include <sys/malloc.h>
     53 #include <sys/pool.h>
     54 
     55 #include <vm/vm.h>
     56 #include <vm/vm_page.h>
     57 #include <vm/vm_kern.h>
     58 
     59 #include <uvm/uvm.h>
     60 
     61 /*
     62  * an aobj manages anonymous-memory backed uvm_objects.   in addition
     63  * to keeping the list of resident pages, it also keeps a list of
     64  * allocated swap blocks.  depending on the size of the aobj this list
     65  * of allocated swap blocks is either stored in an array (small objects)
     66  * or in a hash table (large objects).
     67  */
     68 
     69 /*
     70  * local structures
     71  */
     72 
     73 /*
     74  * for hash tables, we break the address space of the aobj into blocks
     75  * of UAO_SWHASH_CLUSTER_SIZE pages.   we require the cluster size to
     76  * be a power of two.
     77  */
     78 
     79 #define UAO_SWHASH_CLUSTER_SHIFT 4
     80 #define UAO_SWHASH_CLUSTER_SIZE (1 << UAO_SWHASH_CLUSTER_SHIFT)
     81 
     82 /* get the "tag" for this page index */
     83 #define UAO_SWHASH_ELT_TAG(PAGEIDX) \
     84 	((PAGEIDX) >> UAO_SWHASH_CLUSTER_SHIFT)
     85 
     86 /* given an ELT and a page index, find the swap slot */
     87 #define UAO_SWHASH_ELT_PAGESLOT(ELT, PAGEIDX) \
     88 	((ELT)->slots[(PAGEIDX) & (UAO_SWHASH_CLUSTER_SIZE - 1)])
     89 
     90 /* given an ELT, return its pageidx base */
     91 #define UAO_SWHASH_ELT_PAGEIDX_BASE(ELT) \
     92 	((ELT)->tag << UAO_SWHASH_CLUSTER_SHIFT)
     93 
     94 /*
     95  * the swhash hash function
     96  */
     97 #define UAO_SWHASH_HASH(AOBJ, PAGEIDX) \
     98 	(&(AOBJ)->u_swhash[(((PAGEIDX) >> UAO_SWHASH_CLUSTER_SHIFT) \
     99 			    & (AOBJ)->u_swhashmask)])
    100 
    101 /*
    102  * the swhash threshhold determines if we will use an array or a
    103  * hash table to store the list of allocated swap blocks.
    104  */
    105 
    106 #define UAO_SWHASH_THRESHOLD (UAO_SWHASH_CLUSTER_SIZE * 4)
    107 #define UAO_USES_SWHASH(AOBJ) \
    108 	((AOBJ)->u_pages > UAO_SWHASH_THRESHOLD)	/* use hash? */
    109 
    110 /*
    111  * the number of buckets in a swhash, with an upper bound
    112  */
    113 #define UAO_SWHASH_MAXBUCKETS 256
    114 #define UAO_SWHASH_BUCKETS(AOBJ) \
    115 	(min((AOBJ)->u_pages >> UAO_SWHASH_CLUSTER_SHIFT, \
    116 	     UAO_SWHASH_MAXBUCKETS))
    117 
    118 
    119 /*
    120  * uao_swhash_elt: when a hash table is being used, this structure defines
    121  * the format of an entry in the bucket list.
    122  */
    123 
    124 struct uao_swhash_elt {
    125 	LIST_ENTRY(uao_swhash_elt) list;	/* the hash list */
    126 	vaddr_t tag;			/* our 'tag' */
    127 	int count;				/* our number of active slots */
    128 	int slots[UAO_SWHASH_CLUSTER_SIZE];	/* the slots */
    129 };
    130 
    131 /*
    132  * uao_swhash: the swap hash table structure
    133  */
    134 
    135 LIST_HEAD(uao_swhash, uao_swhash_elt);
    136 
    137 /*
    138  * uao_swhash_elt_pool: pool of uao_swhash_elt structures
    139  */
    140 
    141 struct pool uao_swhash_elt_pool;
    142 
    143 /*
    144  * uvm_aobj: the actual anon-backed uvm_object
    145  *
    146  * => the uvm_object is at the top of the structure, this allows
    147  *   (struct uvm_device *) == (struct uvm_object *)
    148  * => only one of u_swslots and u_swhash is used in any given aobj
    149  */
    150 
    151 struct uvm_aobj {
    152 	struct uvm_object u_obj; /* has: lock, pgops, memq, #pages, #refs */
    153 	int u_pages;		 /* number of pages in entire object */
    154 	int u_flags;		 /* the flags (see uvm_aobj.h) */
    155 	int *u_swslots;		 /* array of offset->swapslot mappings */
    156 				 /*
    157 				  * hashtable of offset->swapslot mappings
    158 				  * (u_swhash is an array of bucket heads)
    159 				  */
    160 	struct uao_swhash *u_swhash;
    161 	u_long u_swhashmask;		/* mask for hashtable */
    162 	LIST_ENTRY(uvm_aobj) u_list;	/* global list of aobjs */
    163 };
    164 
    165 /*
    166  * uvm_aobj_pool: pool of uvm_aobj structures
    167  */
    168 
    169 struct pool uvm_aobj_pool;
    170 
    171 /*
    172  * local functions
    173  */
    174 
    175 static void			 uao_init __P((void));
    176 static struct uao_swhash_elt	*uao_find_swhash_elt __P((struct uvm_aobj *,
    177 							  int, boolean_t));
    178 static int			 uao_find_swslot __P((struct uvm_aobj *,
    179 						      int));
    180 static boolean_t		 uao_flush __P((struct uvm_object *,
    181 						vaddr_t, vaddr_t,
    182 						int));
    183 static void			 uao_free __P((struct uvm_aobj *));
    184 static int			 uao_get __P((struct uvm_object *, vaddr_t,
    185 					      vm_page_t *, int *, int,
    186 					      vm_prot_t, int, int));
    187 static boolean_t		 uao_releasepg __P((struct vm_page *,
    188 						    struct vm_page **));
    189 
    190 
    191 
    192 /*
    193  * aobj_pager
    194  *
    195  * note that some functions (e.g. put) are handled elsewhere
    196  */
    197 
    198 struct uvm_pagerops aobj_pager = {
    199 	uao_init,		/* init */
    200 	uao_reference,		/* reference */
    201 	uao_detach,		/* detach */
    202 	NULL,			/* fault */
    203 	uao_flush,		/* flush */
    204 	uao_get,		/* get */
    205 	NULL,			/* asyncget */
    206 	NULL,			/* put (done by pagedaemon) */
    207 	NULL,			/* cluster */
    208 	NULL,			/* mk_pcluster */
    209 	uvm_shareprot,		/* shareprot */
    210 	NULL,			/* aiodone */
    211 	uao_releasepg		/* releasepg */
    212 };
    213 
    214 /*
    215  * uao_list: global list of active aobjs, locked by uao_list_lock
    216  */
    217 
    218 static LIST_HEAD(aobjlist, uvm_aobj) uao_list;
    219 static simple_lock_data_t uao_list_lock;
    220 
    221 
    222 /*
    223  * functions
    224  */
    225 
    226 /*
    227  * hash table/array related functions
    228  */
    229 
    230 /*
    231  * uao_find_swhash_elt: find (or create) a hash table entry for a page
    232  * offset.
    233  *
    234  * => the object should be locked by the caller
    235  */
    236 
    237 static struct uao_swhash_elt *
    238 uao_find_swhash_elt(aobj, pageidx, create)
    239 	struct uvm_aobj *aobj;
    240 	int pageidx;
    241 	boolean_t create;
    242 {
    243 	struct uao_swhash *swhash;
    244 	struct uao_swhash_elt *elt;
    245 	int page_tag;
    246 
    247 	swhash = UAO_SWHASH_HASH(aobj, pageidx); /* first hash to get bucket */
    248 	page_tag = UAO_SWHASH_ELT_TAG(pageidx);	/* tag to search for */
    249 
    250 	/*
    251 	 * now search the bucket for the requested tag
    252 	 */
    253 	for (elt = swhash->lh_first; elt != NULL; elt = elt->list.le_next) {
    254 		if (elt->tag == page_tag)
    255 			return(elt);
    256 	}
    257 
    258 	/* fail now if we are not allowed to create a new entry in the bucket */
    259 	if (!create)
    260 		return NULL;
    261 
    262 
    263 	/*
    264 	 * allocate a new entry for the bucket and init/insert it in
    265 	 */
    266 	elt = pool_get(&uao_swhash_elt_pool, PR_WAITOK);
    267 	LIST_INSERT_HEAD(swhash, elt, list);
    268 	elt->tag = page_tag;
    269 	elt->count = 0;
    270 	memset(elt->slots, 0, sizeof(elt->slots));
    271 
    272 	return(elt);
    273 }
    274 
    275 /*
    276  * uao_find_swslot: find the swap slot number for an aobj/pageidx
    277  *
    278  * => object must be locked by caller
    279  */
    280 __inline static int
    281 uao_find_swslot(aobj, pageidx)
    282 	struct uvm_aobj *aobj;
    283 	int pageidx;
    284 {
    285 
    286 	/*
    287 	 * if noswap flag is set, then we never return a slot
    288 	 */
    289 
    290 	if (aobj->u_flags & UAO_FLAG_NOSWAP)
    291 		return(0);
    292 
    293 	/*
    294 	 * if hashing, look in hash table.
    295 	 */
    296 
    297 	if (UAO_USES_SWHASH(aobj)) {
    298 		struct uao_swhash_elt *elt =
    299 		    uao_find_swhash_elt(aobj, pageidx, FALSE);
    300 
    301 		if (elt)
    302 			return(UAO_SWHASH_ELT_PAGESLOT(elt, pageidx));
    303 		else
    304 			return(NULL);
    305 	}
    306 
    307 	/*
    308 	 * otherwise, look in the array
    309 	 */
    310 	return(aobj->u_swslots[pageidx]);
    311 }
    312 
    313 /*
    314  * uao_set_swslot: set the swap slot for a page in an aobj.
    315  *
    316  * => setting a slot to zero frees the slot
    317  * => object must be locked by caller
    318  */
    319 int
    320 uao_set_swslot(uobj, pageidx, slot)
    321 	struct uvm_object *uobj;
    322 	int pageidx, slot;
    323 {
    324 	struct uvm_aobj *aobj = (struct uvm_aobj *)uobj;
    325 	int oldslot;
    326 	UVMHIST_FUNC("uao_set_swslot"); UVMHIST_CALLED(pdhist);
    327 	UVMHIST_LOG(pdhist, "aobj %p pageidx %d slot %d",
    328 	    aobj, pageidx, slot, 0);
    329 
    330 	/*
    331 	 * if noswap flag is set, then we can't set a slot
    332 	 */
    333 
    334 	if (aobj->u_flags & UAO_FLAG_NOSWAP) {
    335 
    336 		if (slot == 0)
    337 			return(0);		/* a clear is ok */
    338 
    339 		/* but a set is not */
    340 		printf("uao_set_swslot: uobj = %p\n", uobj);
    341 	    panic("uao_set_swslot: attempt to set a slot on a NOSWAP object");
    342 	}
    343 
    344 	/*
    345 	 * are we using a hash table?  if so, add it in the hash.
    346 	 */
    347 
    348 	if (UAO_USES_SWHASH(aobj)) {
    349 		/*
    350 		 * Avoid allocating an entry just to free it again if
    351 		 * the page had not swap slot in the first place, and
    352 		 * we are freeing.
    353 		 */
    354 		struct uao_swhash_elt *elt =
    355 		    uao_find_swhash_elt(aobj, pageidx, slot ? TRUE : FALSE);
    356 		if (elt == NULL) {
    357 #ifdef DIAGNOSTIC
    358 			if (slot)
    359 				panic("uao_set_swslot: didn't create elt");
    360 #endif
    361 			return (0);
    362 		}
    363 
    364 		oldslot = UAO_SWHASH_ELT_PAGESLOT(elt, pageidx);
    365 		UAO_SWHASH_ELT_PAGESLOT(elt, pageidx) = slot;
    366 
    367 		/*
    368 		 * now adjust the elt's reference counter and free it if we've
    369 		 * dropped it to zero.
    370 		 */
    371 
    372 		/* an allocation? */
    373 		if (slot) {
    374 			if (oldslot == 0)
    375 				elt->count++;
    376 		} else {		/* freeing slot ... */
    377 			if (oldslot)	/* to be safe */
    378 				elt->count--;
    379 
    380 			if (elt->count == 0) {
    381 				LIST_REMOVE(elt, list);
    382 				pool_put(&uao_swhash_elt_pool, elt);
    383 			}
    384 		}
    385 
    386 	} else {
    387 		/* we are using an array */
    388 		oldslot = aobj->u_swslots[pageidx];
    389 		aobj->u_swslots[pageidx] = slot;
    390 	}
    391 	return (oldslot);
    392 }
    393 
    394 /*
    395  * end of hash/array functions
    396  */
    397 
    398 /*
    399  * uao_free: free all resources held by an aobj, and then free the aobj
    400  *
    401  * => the aobj should be dead
    402  */
    403 static void
    404 uao_free(aobj)
    405 	struct uvm_aobj *aobj;
    406 {
    407 
    408 	if (UAO_USES_SWHASH(aobj)) {
    409 		int i, hashbuckets = aobj->u_swhashmask + 1;
    410 
    411 		/*
    412 		 * free the swslots from each hash bucket,
    413 		 * then the hash bucket, and finally the hash table itself.
    414 		 */
    415 		for (i = 0; i < hashbuckets; i++) {
    416 			struct uao_swhash_elt *elt, *next;
    417 
    418 			for (elt = aobj->u_swhash[i].lh_first; elt != NULL;
    419 			    elt = next) {
    420 				int j;
    421 
    422 				for (j = 0; j < UAO_SWHASH_CLUSTER_SIZE; j++)
    423 				{
    424 					int slot = elt->slots[j];
    425 
    426 					if (slot) {
    427 						uvm_swap_free(slot, 1);
    428 
    429 						/*
    430 						 * this page is no longer
    431 						 * only in swap.
    432 						 */
    433 						simple_lock(&uvm.swap_data_lock);
    434 						uvmexp.swpgonly--;
    435 						simple_unlock(&uvm.swap_data_lock);
    436 					}
    437 				}
    438 
    439 				next = elt->list.le_next;
    440 				pool_put(&uao_swhash_elt_pool, elt);
    441 			}
    442 		}
    443 		FREE(aobj->u_swhash, M_UVMAOBJ);
    444 	} else {
    445 		int i;
    446 
    447 		/*
    448 		 * free the array
    449 		 */
    450 
    451 		for (i = 0; i < aobj->u_pages; i++)
    452 		{
    453 			int slot = aobj->u_swslots[i];
    454 
    455 			if (slot) {
    456 				uvm_swap_free(slot, 1);
    457 
    458 				/* this page is no longer only in swap. */
    459 				simple_lock(&uvm.swap_data_lock);
    460 				uvmexp.swpgonly--;
    461 				simple_unlock(&uvm.swap_data_lock);
    462 			}
    463 		}
    464 		FREE(aobj->u_swslots, M_UVMAOBJ);
    465 	}
    466 
    467 	/*
    468 	 * finally free the aobj itself
    469 	 */
    470 	pool_put(&uvm_aobj_pool, aobj);
    471 }
    472 
    473 /*
    474  * pager functions
    475  */
    476 
    477 /*
    478  * uao_create: create an aobj of the given size and return its uvm_object.
    479  *
    480  * => for normal use, flags are always zero
    481  * => for the kernel object, the flags are:
    482  *	UAO_FLAG_KERNOBJ - allocate the kernel object (can only happen once)
    483  *	UAO_FLAG_KERNSWAP - enable swapping of kernel object ("           ")
    484  */
    485 struct uvm_object *
    486 uao_create(size, flags)
    487 	vsize_t size;
    488 	int flags;
    489 {
    490 	static struct uvm_aobj kernel_object_store;	/* home of kernel_object */
    491 	static int kobj_alloced = 0;			/* not allocated yet */
    492 	int pages = round_page(size) >> PAGE_SHIFT;
    493 	struct uvm_aobj *aobj;
    494 
    495 	/*
    496  	* malloc a new aobj unless we are asked for the kernel object
    497  	*/
    498 	if (flags & UAO_FLAG_KERNOBJ) {		/* want kernel object? */
    499 		if (kobj_alloced)
    500 			panic("uao_create: kernel object already allocated");
    501 
    502 		/*
    503 		 * XXXTHORPEJ: Need to call this now, so the pool gets
    504 		 * initialized!
    505 		 */
    506 		uao_init();
    507 
    508 		aobj = &kernel_object_store;
    509 		aobj->u_pages = pages;
    510 		aobj->u_flags = UAO_FLAG_NOSWAP;	/* no swap to start */
    511 		/* we are special, we never die */
    512 		aobj->u_obj.uo_refs = UVM_OBJ_KERN;
    513 		kobj_alloced = UAO_FLAG_KERNOBJ;
    514 	} else if (flags & UAO_FLAG_KERNSWAP) {
    515 		aobj = &kernel_object_store;
    516 		if (kobj_alloced != UAO_FLAG_KERNOBJ)
    517 		    panic("uao_create: asked to enable swap on kernel object");
    518 		kobj_alloced = UAO_FLAG_KERNSWAP;
    519 	} else {	/* normal object */
    520 		aobj = pool_get(&uvm_aobj_pool, PR_WAITOK);
    521 		aobj->u_pages = pages;
    522 		aobj->u_flags = 0;		/* normal object */
    523 		aobj->u_obj.uo_refs = 1;	/* start with 1 reference */
    524 	}
    525 
    526 	/*
    527  	 * allocate hash/array if necessary
    528  	 *
    529  	 * note: in the KERNSWAP case no need to worry about locking since
    530  	 * we are still booting we should be the only thread around.
    531  	 */
    532 	if (flags == 0 || (flags & UAO_FLAG_KERNSWAP) != 0) {
    533 		int mflags = (flags & UAO_FLAG_KERNSWAP) != 0 ?
    534 		    M_NOWAIT : M_WAITOK;
    535 
    536 		/* allocate hash table or array depending on object size */
    537 			if (UAO_USES_SWHASH(aobj)) {
    538 			aobj->u_swhash = hashinit(UAO_SWHASH_BUCKETS(aobj),
    539 			    M_UVMAOBJ, mflags, &aobj->u_swhashmask);
    540 			if (aobj->u_swhash == NULL)
    541 				panic("uao_create: hashinit swhash failed");
    542 		} else {
    543 			MALLOC(aobj->u_swslots, int *, pages * sizeof(int),
    544 			    M_UVMAOBJ, mflags);
    545 			if (aobj->u_swslots == NULL)
    546 				panic("uao_create: malloc swslots failed");
    547 			memset(aobj->u_swslots, 0, pages * sizeof(int));
    548 		}
    549 
    550 		if (flags) {
    551 			aobj->u_flags &= ~UAO_FLAG_NOSWAP; /* clear noswap */
    552 			return(&aobj->u_obj);
    553 			/* done! */
    554 		}
    555 	}
    556 
    557 	/*
    558  	 * init aobj fields
    559  	 */
    560 	simple_lock_init(&aobj->u_obj.vmobjlock);
    561 	aobj->u_obj.pgops = &aobj_pager;
    562 	TAILQ_INIT(&aobj->u_obj.memq);
    563 	aobj->u_obj.uo_npages = 0;
    564 
    565 	/*
    566  	 * now that aobj is ready, add it to the global list
    567  	 * XXXCHS: uao_init hasn't been called'd in the KERNOBJ case,
    568 	 * do we really need the kernel object on this list anyway?
    569  	 */
    570 	simple_lock(&uao_list_lock);
    571 	LIST_INSERT_HEAD(&uao_list, aobj, u_list);
    572 	simple_unlock(&uao_list_lock);
    573 
    574 	/*
    575  	 * done!
    576  	 */
    577 	return(&aobj->u_obj);
    578 }
    579 
    580 
    581 
    582 /*
    583  * uao_init: set up aobj pager subsystem
    584  *
    585  * => called at boot time from uvm_pager_init()
    586  */
    587 static void
    588 uao_init()
    589 {
    590 	static int uao_initialized;
    591 
    592 	if (uao_initialized)
    593 		return;
    594 	uao_initialized = TRUE;
    595 
    596 	LIST_INIT(&uao_list);
    597 	simple_lock_init(&uao_list_lock);
    598 
    599 	/*
    600 	 * NOTE: Pages fror this pool must not come from a pageable
    601 	 * kernel map!
    602 	 */
    603 	pool_init(&uao_swhash_elt_pool, sizeof(struct uao_swhash_elt),
    604 	    0, 0, 0, "uaoeltpl", 0, NULL, NULL, M_UVMAOBJ);
    605 
    606 	pool_init(&uvm_aobj_pool, sizeof(struct uvm_aobj), 0, 0, 0,
    607 	    "aobjpl", 0,
    608 	    pool_page_alloc_nointr, pool_page_free_nointr, M_UVMAOBJ);
    609 }
    610 
    611 /*
    612  * uao_reference: add a ref to an aobj
    613  *
    614  * => aobj must be unlocked (we will lock it)
    615  */
    616 void
    617 uao_reference(uobj)
    618 	struct uvm_object *uobj;
    619 {
    620 	UVMHIST_FUNC("uao_reference"); UVMHIST_CALLED(maphist);
    621 
    622 	/*
    623  	 * kernel_object already has plenty of references, leave it alone.
    624  	 */
    625 
    626 	if (UVM_OBJ_IS_KERN_OBJECT(uobj))
    627 		return;
    628 
    629 	simple_lock(&uobj->vmobjlock);
    630 	uobj->uo_refs++;		/* bump! */
    631 	UVMHIST_LOG(maphist, "<- done (uobj=0x%x, ref = %d)",
    632 	uobj, uobj->uo_refs,0,0);
    633 	simple_unlock(&uobj->vmobjlock);
    634 }
    635 
    636 /*
    637  * uao_detach: drop a reference to an aobj
    638  *
    639  * => aobj must be unlocked, we will lock it
    640  */
    641 void
    642 uao_detach(uobj)
    643 	struct uvm_object *uobj;
    644 {
    645 	struct uvm_aobj *aobj = (struct uvm_aobj *)uobj;
    646 	struct vm_page *pg;
    647 	boolean_t busybody;
    648 	UVMHIST_FUNC("uao_detach"); UVMHIST_CALLED(maphist);
    649 
    650 	/*
    651  	 * detaching from kernel_object is a noop.
    652  	 */
    653 	if (UVM_OBJ_IS_KERN_OBJECT(uobj))
    654 		return;
    655 
    656 	simple_lock(&uobj->vmobjlock);
    657 
    658 	UVMHIST_LOG(maphist,"  (uobj=0x%x)  ref=%d", uobj,uobj->uo_refs,0,0);
    659 	uobj->uo_refs--;				/* drop ref! */
    660 	if (uobj->uo_refs) {				/* still more refs? */
    661 		simple_unlock(&uobj->vmobjlock);
    662 		UVMHIST_LOG(maphist, "<- done (rc>0)", 0,0,0,0);
    663 		return;
    664 	}
    665 
    666 	/*
    667  	 * remove the aobj from the global list.
    668  	 */
    669 	simple_lock(&uao_list_lock);
    670 	LIST_REMOVE(aobj, u_list);
    671 	simple_unlock(&uao_list_lock);
    672 
    673 	/*
    674  	 * free all the pages that aren't PG_BUSY, mark for release any that are.
    675  	 */
    676 
    677 	busybody = FALSE;
    678 	for (pg = uobj->memq.tqh_first ; pg != NULL ; pg = pg->listq.tqe_next) {
    679 
    680 		if (pg->flags & PG_BUSY) {
    681 			pg->flags |= PG_RELEASED;
    682 			busybody = TRUE;
    683 			continue;
    684 		}
    685 
    686 		/* zap the mappings, free the swap slot, free the page */
    687 		pmap_page_protect(pg, VM_PROT_NONE);
    688 		uao_dropswap(&aobj->u_obj, pg->offset >> PAGE_SHIFT);
    689 		uvm_lock_pageq();
    690 		uvm_pagefree(pg);
    691 		uvm_unlock_pageq();
    692 	}
    693 
    694 	/*
    695  	 * if we found any busy pages, we're done for now.
    696  	 * mark the aobj for death, releasepg will finish up for us.
    697  	 */
    698 	if (busybody) {
    699 		aobj->u_flags |= UAO_FLAG_KILLME;
    700 		simple_unlock(&aobj->u_obj.vmobjlock);
    701 		return;
    702 	}
    703 
    704 	/*
    705  	 * finally, free the rest.
    706  	 */
    707 	uao_free(aobj);
    708 }
    709 
    710 /*
    711  * uao_flush: "flush" pages out of a uvm object
    712  *
    713  * => object should be locked by caller.  we may _unlock_ the object
    714  *	if (and only if) we need to clean a page (PGO_CLEANIT).
    715  *	XXXJRT Currently, however, we don't.  In the case of cleaning
    716  *	XXXJRT a page, we simply just deactivate it.  Should probably
    717  *	XXXJRT handle this better, in the future (although "flushing"
    718  *	XXXJRT anonymous memory isn't terribly important).
    719  * => if PGO_CLEANIT is not set, then we will neither unlock the object
    720  *	or block.
    721  * => if PGO_ALLPAGE is set, then all pages in the object are valid targets
    722  *	for flushing.
    723  * => NOTE: we rely on the fact that the object's memq is a TAILQ and
    724  *	that new pages are inserted on the tail end of the list.  thus,
    725  *	we can make a complete pass through the object in one go by starting
    726  *	at the head and working towards the tail (new pages are put in
    727  *	front of us).
    728  * => NOTE: we are allowed to lock the page queues, so the caller
    729  *	must not be holding the lock on them [e.g. pagedaemon had
    730  *	better not call us with the queues locked]
    731  * => we return TRUE unless we encountered some sort of I/O error
    732  *	XXXJRT currently never happens, as we never directly initiate
    733  *	XXXJRT I/O
    734  *
    735  * comment on "cleaning" object and PG_BUSY pages:
    736  *	this routine is holding the lock on the object.  the only time
    737  *	that is can run into a PG_BUSY page that it does not own is if
    738  *	some other process has started I/O on the page (e.g. either
    739  *	a pagein or a pageout).  if the PG_BUSY page is being paged
    740  *	in, then it can not be dirty (!PG_CLEAN) because no one has
    741  *	had a change to modify it yet.  if the PG_BUSY page is being
    742  *	paged out then it means that someone else has already started
    743  *	cleaning the page for us (how nice!).  in this case, if we
    744  *	have syncio specified, then after we make our pass through the
    745  *	object we need to wait for the other PG_BUSY pages to clear
    746  *	off (i.e. we need to do an iosync).  also note that once a
    747  *	page is PG_BUSY is must stary in its object until it is un-busyed.
    748  *	XXXJRT We never actually do this, as we are "flushing" anonymous
    749  *	XXXJRT memory, which doesn't have persistent backing store.
    750  *
    751  * note on page traversal:
    752  *	we can traverse the pages in an object either by going down the
    753  *	linked list in "uobj->memq", or we can go over the address range
    754  *	by page doing hash table lookups for each address.  depending
    755  *	on how many pages are in the object it may be cheaper to do one
    756  *	or the other.  we set "by_list" to true if we are using memq.
    757  *	if the cost of a hash lookup was equal to the cost of the list
    758  *	traversal we could compare the number of pages in the start->stop
    759  *	range to the total number of pages in the object.  however, it
    760  *	seems that a hash table lookup is more expensive than the linked
    761  *	list traversal, so we multiply the number of pages in the
    762  *	start->stop range by a penalty which we define below.
    763  */
    764 
    765 #define	UAO_HASH_PENALTY 4	/* XXX: a guess */
    766 
    767 boolean_t
    768 uao_flush(uobj, start, stop, flags)
    769 	struct uvm_object *uobj;
    770 	vaddr_t start, stop;
    771 	int flags;
    772 {
    773 	struct uvm_aobj *aobj = (struct uvm_aobj *) uobj;
    774 	struct vm_page *pp, *ppnext;
    775 	boolean_t retval, by_list;
    776 	vaddr_t curoff;
    777 	UVMHIST_FUNC("uao_flush"); UVMHIST_CALLED(maphist);
    778 
    779 	curoff = 0;	/* XXX: shut up gcc */
    780 
    781 	retval = TRUE;	/* default to success */
    782 
    783 	if (flags & PGO_ALLPAGES) {
    784 		start = 0;
    785 		stop = aobj->u_pages << PAGE_SHIFT;
    786 		by_list = TRUE;		/* always go by the list */
    787 	} else {
    788 		start = trunc_page(start);
    789 		stop = round_page(stop);
    790 		if (stop > (aobj->u_pages << PAGE_SHIFT)) {
    791 			printf("uao_flush: strange, got an out of range "
    792 			    "flush (fixed)\n");
    793 			stop = aobj->u_pages << PAGE_SHIFT;
    794 		}
    795 		by_list = (uobj->uo_npages <=
    796 		    ((stop - start) >> PAGE_SHIFT) * UAO_HASH_PENALTY);
    797 	}
    798 
    799 	UVMHIST_LOG(maphist,
    800 	    " flush start=0x%lx, stop=0x%x, by_list=%d, flags=0x%x",
    801 	    start, stop, by_list, flags);
    802 
    803 	/*
    804 	 * Don't need to do any work here if we're not freeing
    805 	 * or deactivating pages.
    806 	 */
    807 	if ((flags & (PGO_DEACTIVATE|PGO_FREE)) == 0) {
    808 		UVMHIST_LOG(maphist,
    809 		    "<- done (no work to do)",0,0,0,0);
    810 		return (retval);
    811 	}
    812 
    813 	/*
    814 	 * now do it.  note: we must update ppnext in the body of loop or we
    815 	 * will get stuck.  we need to use ppnext because we may free "pp"
    816 	 * before doing the next loop.
    817 	 */
    818 
    819 	if (by_list) {
    820 		pp = uobj->memq.tqh_first;
    821 	} else {
    822 		curoff = start;
    823 		pp = uvm_pagelookup(uobj, curoff);
    824 	}
    825 
    826 	ppnext = NULL;	/* XXX: shut up gcc */
    827 	uvm_lock_pageq();	/* page queues locked */
    828 
    829 	/* locked: both page queues and uobj */
    830 	for ( ; (by_list && pp != NULL) ||
    831 	    (!by_list && curoff < stop) ; pp = ppnext) {
    832 		if (by_list) {
    833 			ppnext = pp->listq.tqe_next;
    834 
    835 			/* range check */
    836 			if (pp->offset < start || pp->offset >= stop)
    837 				continue;
    838 		} else {
    839 			curoff += PAGE_SIZE;
    840 			if (curoff < stop)
    841 				ppnext = uvm_pagelookup(uobj, curoff);
    842 
    843 			/* null check */
    844 			if (pp == NULL)
    845 				continue;
    846 		}
    847 
    848 		switch (flags & (PGO_CLEANIT|PGO_FREE|PGO_DEACTIVATE)) {
    849 		/*
    850 		 * XXX In these first 3 cases, we always just
    851 		 * XXX deactivate the page.  We may want to
    852 		 * XXX handle the different cases more specifically
    853 		 * XXX in the future.
    854 		 */
    855 		case PGO_CLEANIT|PGO_FREE:
    856 		case PGO_CLEANIT|PGO_DEACTIVATE:
    857 		case PGO_DEACTIVATE:
    858  deactivate_it:
    859 			/* skip the page if it's loaned or wired */
    860 			if (pp->loan_count != 0 ||
    861 			    pp->wire_count != 0)
    862 				continue;
    863 
    864 			/* zap all mappings for the page. */
    865 			pmap_page_protect(pp, VM_PROT_NONE);
    866 
    867 			/* ...and deactivate the page. */
    868 			uvm_pagedeactivate(pp);
    869 
    870 			continue;
    871 
    872 		case PGO_FREE:
    873 			/*
    874 			 * If there are multiple references to
    875 			 * the object, just deactivate the page.
    876 			 */
    877 			if (uobj->uo_refs > 1)
    878 				goto deactivate_it;
    879 
    880 			/* XXX skip the page if it's loaned or wired */
    881 			if (pp->loan_count != 0 ||
    882 			    pp->wire_count != 0)
    883 				continue;
    884 
    885 			/*
    886 			 * mark the page as released if its busy.
    887 			 */
    888 			if (pp->flags & PG_BUSY) {
    889 				pp->flags |= PG_RELEASED;
    890 				continue;
    891 			}
    892 
    893 			/* zap all mappings for the page. */
    894 			pmap_page_protect(pp, VM_PROT_NONE);
    895 
    896 			uao_dropswap(uobj, pp->offset >> PAGE_SHIFT);
    897 			uvm_pagefree(pp);
    898 
    899 			continue;
    900 
    901 		default:
    902 			panic("uao_flush: weird flags");
    903 		}
    904 #ifdef DIAGNOSTIC
    905 		panic("uao_flush: unreachable code");
    906 #endif
    907 	}
    908 
    909 	uvm_unlock_pageq();
    910 
    911 	UVMHIST_LOG(maphist,
    912 	    "<- done, rv=%d",retval,0,0,0);
    913 	return (retval);
    914 }
    915 
    916 /*
    917  * uao_get: fetch me a page
    918  *
    919  * we have three cases:
    920  * 1: page is resident     -> just return the page.
    921  * 2: page is zero-fill    -> allocate a new page and zero it.
    922  * 3: page is swapped out  -> fetch the page from swap.
    923  *
    924  * cases 1 and 2 can be handled with PGO_LOCKED, case 3 cannot.
    925  * so, if the "center" page hits case 3 (or any page, with PGO_ALLPAGES),
    926  * then we will need to return VM_PAGER_UNLOCK.
    927  *
    928  * => prefer map unlocked (not required)
    929  * => object must be locked!  we will _unlock_ it before starting any I/O.
    930  * => flags: PGO_ALLPAGES: get all of the pages
    931  *           PGO_LOCKED: fault data structures are locked
    932  * => NOTE: offset is the offset of pps[0], _NOT_ pps[centeridx]
    933  * => NOTE: caller must check for released pages!!
    934  */
    935 static int
    936 uao_get(uobj, offset, pps, npagesp, centeridx, access_type, advice, flags)
    937 	struct uvm_object *uobj;
    938 	vaddr_t offset;
    939 	struct vm_page **pps;
    940 	int *npagesp;
    941 	int centeridx, advice, flags;
    942 	vm_prot_t access_type;
    943 {
    944 	struct uvm_aobj *aobj = (struct uvm_aobj *)uobj;
    945 	vaddr_t current_offset;
    946 	vm_page_t ptmp;
    947 	int lcv, gotpages, maxpages, swslot, rv;
    948 	boolean_t done;
    949 	UVMHIST_FUNC("uao_get"); UVMHIST_CALLED(pdhist);
    950 
    951 	UVMHIST_LOG(pdhist, "aobj=%p offset=%d, flags=%d", aobj, offset, flags,0);
    952 
    953 	/*
    954  	 * get number of pages
    955  	 */
    956 
    957 	maxpages = *npagesp;
    958 
    959 	/*
    960  	 * step 1: handled the case where fault data structures are locked.
    961  	 */
    962 
    963 	if (flags & PGO_LOCKED) {
    964 
    965 		/*
    966  		 * step 1a: get pages that are already resident.   only do
    967 		 * this if the data structures are locked (i.e. the first
    968 		 * time through).
    969  		 */
    970 
    971 		done = TRUE;	/* be optimistic */
    972 		gotpages = 0;	/* # of pages we got so far */
    973 
    974 		for (lcv = 0, current_offset = offset ; lcv < maxpages ;
    975 		    lcv++, current_offset += PAGE_SIZE) {
    976 			/* do we care about this page?  if not, skip it */
    977 			if (pps[lcv] == PGO_DONTCARE)
    978 				continue;
    979 
    980 			ptmp = uvm_pagelookup(uobj, current_offset);
    981 
    982 			/*
    983  			 * if page is new, attempt to allocate the page, then
    984 			 * zero-fill it.
    985  			 */
    986 			if (ptmp == NULL && uao_find_swslot(aobj,
    987 			    current_offset >> PAGE_SHIFT) == 0) {
    988 				ptmp = uvm_pagealloc(uobj, current_offset,
    989 				    NULL, 0);
    990 				if (ptmp) {
    991 					/* new page */
    992 					ptmp->flags &= ~(PG_BUSY|PG_FAKE);
    993 					ptmp->pqflags |= PQ_AOBJ;
    994 					UVM_PAGE_OWN(ptmp, NULL);
    995 					uvm_pagezero(ptmp);
    996 				}
    997 			}
    998 
    999 			/*
   1000 			 * to be useful must get a non-busy, non-released page
   1001 			 */
   1002 			if (ptmp == NULL ||
   1003 			    (ptmp->flags & (PG_BUSY|PG_RELEASED)) != 0) {
   1004 				if (lcv == centeridx ||
   1005 				    (flags & PGO_ALLPAGES) != 0)
   1006 					/* need to do a wait or I/O! */
   1007 					done = FALSE;
   1008 					continue;
   1009 			}
   1010 
   1011 			/*
   1012 			 * useful page: busy/lock it and plug it in our
   1013 			 * result array
   1014 			 */
   1015 			/* caller must un-busy this page */
   1016 			ptmp->flags |= PG_BUSY;
   1017 			UVM_PAGE_OWN(ptmp, "uao_get1");
   1018 			pps[lcv] = ptmp;
   1019 			gotpages++;
   1020 
   1021 		}	/* "for" lcv loop */
   1022 
   1023 		/*
   1024  		 * step 1b: now we've either done everything needed or we
   1025 		 * to unlock and do some waiting or I/O.
   1026  		 */
   1027 
   1028 		UVMHIST_LOG(pdhist, "<- done (done=%d)", done, 0,0,0);
   1029 
   1030 		*npagesp = gotpages;
   1031 		if (done)
   1032 			/* bingo! */
   1033 			return(VM_PAGER_OK);
   1034 		else
   1035 			/* EEK!   Need to unlock and I/O */
   1036 			return(VM_PAGER_UNLOCK);
   1037 	}
   1038 
   1039 	/*
   1040  	 * step 2: get non-resident or busy pages.
   1041  	 * object is locked.   data structures are unlocked.
   1042  	 */
   1043 
   1044 	for (lcv = 0, current_offset = offset ; lcv < maxpages ;
   1045 	    lcv++, current_offset += PAGE_SIZE) {
   1046 		/*
   1047 		 * - skip over pages we've already gotten or don't want
   1048 		 * - skip over pages we don't _have_ to get
   1049 		 */
   1050 		if (pps[lcv] != NULL ||
   1051 		    (lcv != centeridx && (flags & PGO_ALLPAGES) == 0))
   1052 			continue;
   1053 
   1054 		/*
   1055  		 * we have yet to locate the current page (pps[lcv]).   we
   1056 		 * first look for a page that is already at the current offset.
   1057 		 * if we find a page, we check to see if it is busy or
   1058 		 * released.  if that is the case, then we sleep on the page
   1059 		 * until it is no longer busy or released and repeat the lookup.
   1060 		 * if the page we found is neither busy nor released, then we
   1061 		 * busy it (so we own it) and plug it into pps[lcv].   this
   1062 		 * 'break's the following while loop and indicates we are
   1063 		 * ready to move on to the next page in the "lcv" loop above.
   1064  		 *
   1065  		 * if we exit the while loop with pps[lcv] still set to NULL,
   1066 		 * then it means that we allocated a new busy/fake/clean page
   1067 		 * ptmp in the object and we need to do I/O to fill in the data.
   1068  		 */
   1069 
   1070 		/* top of "pps" while loop */
   1071 		while (pps[lcv] == NULL) {
   1072 			/* look for a resident page */
   1073 			ptmp = uvm_pagelookup(uobj, current_offset);
   1074 
   1075 			/* not resident?   allocate one now (if we can) */
   1076 			if (ptmp == NULL) {
   1077 
   1078 				ptmp = uvm_pagealloc(uobj, current_offset,
   1079 				    NULL, 0);
   1080 
   1081 				/* out of RAM? */
   1082 				if (ptmp == NULL) {
   1083 					simple_unlock(&uobj->vmobjlock);
   1084 					UVMHIST_LOG(pdhist,
   1085 					    "sleeping, ptmp == NULL\n",0,0,0,0);
   1086 					uvm_wait("uao_getpage");
   1087 					simple_lock(&uobj->vmobjlock);
   1088 					/* goto top of pps while loop */
   1089 					continue;
   1090 				}
   1091 
   1092 				/*
   1093 				 * safe with PQ's unlocked: because we just
   1094 				 * alloc'd the page
   1095 				 */
   1096 				ptmp->pqflags |= PQ_AOBJ;
   1097 
   1098 				/*
   1099 				 * got new page ready for I/O.  break pps while
   1100 				 * loop.  pps[lcv] is still NULL.
   1101 				 */
   1102 				break;
   1103 			}
   1104 
   1105 			/* page is there, see if we need to wait on it */
   1106 			if ((ptmp->flags & (PG_BUSY|PG_RELEASED)) != 0) {
   1107 				ptmp->flags |= PG_WANTED;
   1108 				UVMHIST_LOG(pdhist,
   1109 				    "sleeping, ptmp->flags 0x%x\n",
   1110 				    ptmp->flags,0,0,0);
   1111 				UVM_UNLOCK_AND_WAIT(ptmp, &uobj->vmobjlock,
   1112 				    FALSE, "uao_get", 0);
   1113 				simple_lock(&uobj->vmobjlock);
   1114 				continue;	/* goto top of pps while loop */
   1115 			}
   1116 
   1117 			/*
   1118  			 * if we get here then the page has become resident and
   1119 			 * unbusy between steps 1 and 2.  we busy it now (so we
   1120 			 * own it) and set pps[lcv] (so that we exit the while
   1121 			 * loop).
   1122  			 */
   1123 			/* we own it, caller must un-busy */
   1124 			ptmp->flags |= PG_BUSY;
   1125 			UVM_PAGE_OWN(ptmp, "uao_get2");
   1126 			pps[lcv] = ptmp;
   1127 		}
   1128 
   1129 		/*
   1130  		 * if we own the valid page at the correct offset, pps[lcv] will
   1131  		 * point to it.   nothing more to do except go to the next page.
   1132  		 */
   1133 		if (pps[lcv])
   1134 			continue;			/* next lcv */
   1135 
   1136 		/*
   1137  		 * we have a "fake/busy/clean" page that we just allocated.
   1138  		 * do the needed "i/o", either reading from swap or zeroing.
   1139  		 */
   1140 		swslot = uao_find_swslot(aobj, current_offset >> PAGE_SHIFT);
   1141 
   1142 		/*
   1143  		 * just zero the page if there's nothing in swap.
   1144  		 */
   1145 		if (swslot == 0)
   1146 		{
   1147 			/*
   1148 			 * page hasn't existed before, just zero it.
   1149 			 */
   1150 			uvm_pagezero(ptmp);
   1151 		}
   1152 		else
   1153 		{
   1154 			UVMHIST_LOG(pdhist, "pagein from swslot %d",
   1155 			     swslot, 0,0,0);
   1156 
   1157 			/*
   1158 			 * page in the swapped-out page.
   1159 			 * unlock object for i/o, relock when done.
   1160 			 */
   1161 			simple_unlock(&uobj->vmobjlock);
   1162 			rv = uvm_swap_get(ptmp, swslot, PGO_SYNCIO);
   1163 			simple_lock(&uobj->vmobjlock);
   1164 
   1165 			/*
   1166 			 * I/O done.  check for errors.
   1167 			 */
   1168 			if (rv != VM_PAGER_OK)
   1169 			{
   1170 				UVMHIST_LOG(pdhist, "<- done (error=%d)",
   1171 				    rv,0,0,0);
   1172 				if (ptmp->flags & PG_WANTED)
   1173 					/* object lock still held */
   1174 					wakeup(ptmp);
   1175 				ptmp->flags &= ~(PG_WANTED|PG_BUSY);
   1176 				UVM_PAGE_OWN(ptmp, NULL);
   1177 				uvm_lock_pageq();
   1178 				uvm_pagefree(ptmp);
   1179 				uvm_unlock_pageq();
   1180 				simple_unlock(&uobj->vmobjlock);
   1181 				return (rv);
   1182 			}
   1183 		}
   1184 
   1185 		/*
   1186  		 * we got the page!   clear the fake flag (indicates valid
   1187 		 * data now in page) and plug into our result array.   note
   1188 		 * that page is still busy.
   1189  		 *
   1190  		 * it is the callers job to:
   1191  		 * => check if the page is released
   1192  		 * => unbusy the page
   1193  		 * => activate the page
   1194  		 */
   1195 
   1196 		ptmp->flags &= ~PG_FAKE;		/* data is valid ... */
   1197 		pmap_clear_modify(ptmp);		/* ... and clean */
   1198 		pps[lcv] = ptmp;
   1199 
   1200 	}	/* lcv loop */
   1201 
   1202 	/*
   1203  	 * finally, unlock object and return.
   1204  	 */
   1205 
   1206 	simple_unlock(&uobj->vmobjlock);
   1207 	UVMHIST_LOG(pdhist, "<- done (OK)",0,0,0,0);
   1208 	return(VM_PAGER_OK);
   1209 }
   1210 
   1211 /*
   1212  * uao_releasepg: handle released page in an aobj
   1213  *
   1214  * => "pg" is a PG_BUSY [caller owns it], PG_RELEASED page that we need
   1215  *      to dispose of.
   1216  * => caller must handle PG_WANTED case
   1217  * => called with page's object locked, pageq's unlocked
   1218  * => returns TRUE if page's object is still alive, FALSE if we
   1219  *      killed the page's object.    if we return TRUE, then we
   1220  *      return with the object locked.
   1221  * => if (nextpgp != NULL) => we return pageq.tqe_next here, and return
   1222  *                              with the page queues locked [for pagedaemon]
   1223  * => if (nextpgp == NULL) => we return with page queues unlocked [normal case]
   1224  * => we kill the aobj if it is not referenced and we are suppose to
   1225  *      kill it ("KILLME").
   1226  */
   1227 static boolean_t uao_releasepg(pg, nextpgp)
   1228 	struct vm_page *pg;
   1229 	struct vm_page **nextpgp;	/* OUT */
   1230 {
   1231 	struct uvm_aobj *aobj = (struct uvm_aobj *) pg->uobject;
   1232 
   1233 #ifdef DIAGNOSTIC
   1234 	if ((pg->flags & PG_RELEASED) == 0)
   1235 		panic("uao_releasepg: page not released!");
   1236 #endif
   1237 
   1238 	/*
   1239  	 * dispose of the page [caller handles PG_WANTED] and swap slot.
   1240  	 */
   1241 	pmap_page_protect(pg, VM_PROT_NONE);
   1242 	uao_dropswap(&aobj->u_obj, pg->offset >> PAGE_SHIFT);
   1243 	uvm_lock_pageq();
   1244 	if (nextpgp)
   1245 		*nextpgp = pg->pageq.tqe_next;	/* next page for daemon */
   1246 	uvm_pagefree(pg);
   1247 	if (!nextpgp)
   1248 		uvm_unlock_pageq();			/* keep locked for daemon */
   1249 
   1250 	/*
   1251  	 * if we're not killing the object, we're done.
   1252  	 */
   1253 	if ((aobj->u_flags & UAO_FLAG_KILLME) == 0)
   1254 		return TRUE;
   1255 
   1256 #ifdef DIAGNOSTIC
   1257 	if (aobj->u_obj.uo_refs)
   1258 		panic("uvm_km_releasepg: kill flag set on referenced object!");
   1259 #endif
   1260 
   1261 	/*
   1262  	 * if there are still pages in the object, we're done for now.
   1263  	 */
   1264 	if (aobj->u_obj.uo_npages != 0)
   1265 		return TRUE;
   1266 
   1267 #ifdef DIAGNOSTIC
   1268 	if (aobj->u_obj.memq.tqh_first)
   1269 		panic("uvn_releasepg: pages in object with npages == 0");
   1270 #endif
   1271 
   1272 	/*
   1273  	 * finally, free the rest.
   1274  	 */
   1275 	uao_free(aobj);
   1276 
   1277 	return FALSE;
   1278 }
   1279 
   1280 /*
   1281  * uao_dropswap:  release any swap resources from this aobj page.
   1282  *
   1283  * => aobj must be locked or have a reference count of 0.
   1284  */
   1285 
   1286 void
   1287 uao_dropswap(uobj, pageidx)
   1288 	struct uvm_object *uobj;
   1289 	int pageidx;
   1290 {
   1291 	int slot;
   1292 
   1293 	slot = uao_set_swslot(uobj, pageidx, 0);
   1294 	if (slot) {
   1295 		uvm_swap_free(slot, 1);
   1296 	}
   1297 }
   1298