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