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