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