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