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