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