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