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