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