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