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