No empty .Rs/.Re
[netbsd-mini2440.git] / sys / uvm / uvm_aobj.c
blob6ae2faeae930b6ea56f247e0275272894feafc76
1 /* $NetBSD: uvm_aobj.c,v 1.107 2009/09/13 18:45:12 pooka Exp $ */
3 /*
4 * Copyright (c) 1998 Chuck Silvers, Charles D. Cranor and
5 * Washington University.
6 * All rights reserved.
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.
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.
34 * from: Id: uvm_aobj.c,v 1.1.2.5 1998/02/06 05:14:38 chs Exp
37 * uvm_aobj.c: anonymous memory uvm_object pager
39 * author: Chuck Silvers <chuq@chuq.com>
40 * started: Jan-1998
42 * - design mostly from Chuck Cranor
45 #include <sys/cdefs.h>
46 __KERNEL_RCSID(0, "$NetBSD: uvm_aobj.c,v 1.107 2009/09/13 18:45:12 pooka Exp $");
48 #include "opt_uvmhist.h"
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>
57 #include <uvm/uvm.h>
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).
68 * local structures
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.
77 #define UAO_SWHASH_CLUSTER_SHIFT 4
78 #define UAO_SWHASH_CLUSTER_SIZE (1 << UAO_SWHASH_CLUSTER_SHIFT)
80 /* get the "tag" for this page index */
81 #define UAO_SWHASH_ELT_TAG(PAGEIDX) \
82 ((PAGEIDX) >> UAO_SWHASH_CLUSTER_SHIFT)
84 #define UAO_SWHASH_ELT_PAGESLOT_IDX(PAGEIDX) \
85 ((PAGEIDX) & (UAO_SWHASH_CLUSTER_SIZE - 1))
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)])
91 /* given an ELT, return its pageidx base */
92 #define UAO_SWHASH_ELT_PAGEIDX_BASE(ELT) \
93 ((ELT)->tag << UAO_SWHASH_CLUSTER_SHIFT)
96 * the swhash hash function
99 #define UAO_SWHASH_HASH(AOBJ, PAGEIDX) \
100 (&(AOBJ)->u_swhash[(((PAGEIDX) >> UAO_SWHASH_CLUSTER_SHIFT) \
101 & (AOBJ)->u_swhashmask)])
104 * the swhash threshhold determines if we will use an array or a
105 * hash table to store the list of allocated swap blocks.
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? */
113 * the number of buckets in a swhash, with an upper bound
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))
122 * uao_swhash_elt: when a hash table is being used, this structure defines
123 * the format of an entry in the bucket list.
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 */
134 * uao_swhash: the swap hash table structure
137 LIST_HEAD(uao_swhash, uao_swhash_elt);
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!
143 static struct pool uao_swhash_elt_pool;
145 static struct pool_cache uvm_aobj_cache;
148 * uvm_aobj: the actual anon-backed uvm_object
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
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 */
161 * hashtable of offset->swapslot mappings
162 * (u_swhash is an array of bucket heads)
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 */
170 * local functions
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);
178 static void uao_detach_locked(struct uvm_object *);
179 static void uao_reference_locked(struct uvm_object *);
181 #if defined(VMSWAP)
182 static struct uao_swhash_elt *uao_find_swhash_elt
183 (struct uvm_aobj *, int, bool);
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) */
191 * aobj_pager
193 * note that some functions (e.g. put) are handled elsewhere
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,
204 * uao_list: global list of active aobjs, locked by uao_list_lock
207 static LIST_HEAD(aobjlist, uvm_aobj) uao_list;
208 static kmutex_t uao_list_lock;
211 * functions
215 * hash table/array related functions
218 #if defined(VMSWAP)
221 * uao_find_swhash_elt: find (or create) a hash table entry for a page
222 * offset.
224 * => the object should be locked by the caller
227 static struct uao_swhash_elt *
228 uao_find_swhash_elt(struct uvm_aobj *aobj, int pageidx, bool create)
230 struct uao_swhash *swhash;
231 struct uao_swhash_elt *elt;
232 voff_t page_tag;
234 swhash = UAO_SWHASH_HASH(aobj, pageidx);
235 page_tag = UAO_SWHASH_ELT_TAG(pageidx);
238 * now search the bucket for the requested tag
241 LIST_FOREACH(elt, swhash, list) {
242 if (elt->tag == page_tag) {
243 return elt;
246 if (!create) {
247 return NULL;
251 * allocate a new entry for the bucket and init/insert it in
254 elt = pool_get(&uao_swhash_elt_pool, PR_NOWAIT);
255 if (elt == NULL) {
256 return NULL;
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;
266 * uao_find_swslot: find the swap slot number for an aobj/pageidx
268 * => object must be locked by caller
272 uao_find_swslot(struct uvm_object *uobj, int pageidx)
274 struct uvm_aobj *aobj = (struct uvm_aobj *)uobj;
275 struct uao_swhash_elt *elt;
278 * if noswap flag is set, then we never return a slot
281 if (aobj->u_flags & UAO_FLAG_NOSWAP)
282 return(0);
285 * if hashing, look in hash table.
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);
297 * otherwise, look in the array
300 return(aobj->u_swslots[pageidx]);
304 * uao_set_swslot: set the swap slot for a page in an aobj.
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
313 uao_set_swslot(struct uvm_object *uobj, int pageidx, int slot)
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);
323 * if noswap flag is set, then we can't set a non-zero slot.
326 if (aobj->u_flags & UAO_FLAG_NOSWAP) {
327 if (slot == 0)
328 return(0);
330 printf("uao_set_swslot: uobj = %p\n", uobj);
331 panic("uao_set_swslot: NOSWAP object");
335 * are we using a hash table? if so, add it in the hash.
338 if (UAO_USES_SWHASH(aobj)) {
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.
346 elt = uao_find_swhash_elt(aobj, pageidx, slot != 0);
347 if (elt == NULL) {
348 return slot ? -1 : 0;
351 oldslot = UAO_SWHASH_ELT_PAGESLOT(elt, pageidx);
352 UAO_SWHASH_ELT_PAGESLOT(elt, pageidx) = slot;
355 * now adjust the elt's reference counter and free it if we've
356 * dropped it to zero.
359 if (slot) {
360 if (oldslot == 0)
361 elt->count++;
362 } else {
363 if (oldslot)
364 elt->count--;
366 if (elt->count == 0) {
367 LIST_REMOVE(elt, list);
368 pool_put(&uao_swhash_elt_pool, elt);
371 } else {
372 /* we are using an array */
373 oldslot = aobj->u_swslots[pageidx];
374 aobj->u_swslots[pageidx] = slot;
376 return (oldslot);
379 #endif /* defined(VMSWAP) */
382 * end of hash/array functions
386 * uao_free: free all resources held by an aobj, and then free the aobj
388 * => the aobj should be dead
391 static void
392 uao_free(struct uvm_aobj *aobj)
394 int swpgonlydelta = 0;
397 #if defined(VMSWAP)
398 uao_dropswap_range1(aobj, 0, 0);
399 #endif /* defined(VMSWAP) */
401 mutex_exit(&aobj->u_obj.vmobjlock);
403 #if defined(VMSWAP)
404 if (UAO_USES_SWHASH(aobj)) {
407 * free the hash table itself.
410 hashdone(aobj->u_swhash, HASH_LIST, aobj->u_swhashmask);
411 } else {
414 * free the array itsself.
417 kmem_free(aobj->u_swslots, aobj->u_pages * sizeof(int));
419 #endif /* defined(VMSWAP) */
422 * finally free the aobj itself
425 UVM_OBJ_DESTROY(&aobj->u_obj);
426 pool_cache_put(&uvm_aobj_cache, aobj);
429 * adjust the counter of pages only in swap for all
430 * the swap slots we've freed.
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);
442 * pager functions
446 * uao_create: create an aobj of the given size and return its uvm_object.
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 (" ")
454 struct uvm_object *
455 uao_create(vsize_t size, int flags)
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;
464 * malloc a new aobj unless we are asked for the kernel object
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;
487 * allocate hash/array if necessary
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.
493 if (flags == 0 || (flags & UAO_FLAG_KERNSWAP) != 0) {
494 #if defined(VMSWAP)
495 const int kernswap = (flags & UAO_FLAG_KERNSWAP) != 0;
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");
510 #endif /* defined(VMSWAP) */
512 if (flags) {
513 aobj->u_flags &= ~UAO_FLAG_NOSWAP; /* clear noswap */
514 return(&aobj->u_obj);
519 * init aobj fields
522 UVM_OBJ_INIT(&aobj->u_obj, &aobj_pager, refs);
525 * now that aobj is ready, add it to the global list
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);
537 * uao_init: set up aobj pager subsystem
539 * => called at boot time from uvm_pager_init()
542 void
543 uao_init(void)
545 static int uao_initialized;
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);
559 * uao_reference: add a ref to an aobj
561 * => aobj must be unlocked
562 * => just lock it and call the locked version
565 void
566 uao_reference(struct uvm_object *uobj)
570 * kernel_object already has plenty of references, leave it alone.
573 if (UVM_OBJ_IS_KERN_OBJECT(uobj))
574 return;
576 mutex_enter(&uobj->vmobjlock);
577 uao_reference_locked(uobj);
578 mutex_exit(&uobj->vmobjlock);
582 * uao_reference_locked: add a ref to an aobj that is already locked
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.
590 static void
591 uao_reference_locked(struct uvm_object *uobj)
593 UVMHIST_FUNC("uao_reference"); UVMHIST_CALLED(maphist);
596 * kernel_object already has plenty of references, leave it alone.
599 if (UVM_OBJ_IS_KERN_OBJECT(uobj))
600 return;
602 uobj->uo_refs++;
603 UVMHIST_LOG(maphist, "<- done (uobj=0x%x, ref = %d)",
604 uobj, uobj->uo_refs,0,0);
608 * uao_detach: drop a reference to an aobj
610 * => aobj must be unlocked
611 * => just lock it and call the locked version
614 void
615 uao_detach(struct uvm_object *uobj)
619 * detaching from kernel_object is a noop.
622 if (UVM_OBJ_IS_KERN_OBJECT(uobj))
623 return;
625 mutex_enter(&uobj->vmobjlock);
626 uao_detach_locked(uobj);
630 * uao_detach_locked: drop a reference to an aobj
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.
638 static void
639 uao_detach_locked(struct uvm_object *uobj)
641 struct uvm_aobj *aobj = (struct uvm_aobj *)uobj;
642 struct vm_page *pg;
643 UVMHIST_FUNC("uao_detach"); UVMHIST_CALLED(maphist);
646 * detaching from kernel_object is a noop.
649 if (UVM_OBJ_IS_KERN_OBJECT(uobj)) {
650 mutex_exit(&uobj->vmobjlock);
651 return;
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;
663 * remove the aobj from the global list.
666 mutex_enter(&uao_list_lock);
667 LIST_REMOVE(aobj, u_list);
668 mutex_exit(&uao_list_lock);
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.
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;
689 uao_dropswap(&aobj->u_obj, pg->offset >> PAGE_SHIFT);
690 uvm_pagefree(pg);
692 mutex_exit(&uvm_pageqlock);
695 * finally, free the aobj itself.
698 uao_free(aobj);
702 * uao_put: flush pages out of a uvm object
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
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.
740 static int
741 uao_put(struct uvm_object *uobj, voff_t start, voff_t stop, int flags)
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);
749 KASSERT(mutex_owned(&uobj->vmobjlock));
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);
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;
768 by_list = (uobj->uo_npages <=
769 ((stop - start) >> PAGE_SHIFT) * UVM_PAGE_TREE_PENALTY);
771 UVMHIST_LOG(maphist,
772 " flush start=0x%lx, stop=0x%x, by_list=%d, flags=0x%x",
773 start, stop, by_list, flags);
776 * Don't need to do any work here if we're not freeing
777 * or deactivating pages.
780 if ((flags & (PGO_DEACTIVATE|PGO_FREE)) == 0) {
781 mutex_exit(&uobj->vmobjlock);
782 return 0;
786 * Initialize the marker pages. See the comment in
787 * genfs_putpages() also.
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;
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.
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 */
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;
831 * wait and try again if the page is busy.
834 if (pg->flags & PG_BUSY) {
835 if (by_list) {
836 TAILQ_INSERT_BEFORE(pg, &curmp, listq.queue);
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;
851 switch (flags & (PGO_CLEANIT|PGO_FREE|PGO_DEACTIVATE)) {
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.
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);
869 mutex_exit(&uvm_pageqlock);
870 break;
872 case PGO_FREE:
874 * If there are multiple references to
875 * the object, just deactivate the page.
878 if (uobj->uo_refs > 1)
879 goto deactivate_it;
882 * free the swap slot and the page.
885 pmap_page_protect(pg, VM_PROT_NONE);
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.
893 uao_dropswap(uobj, pg->offset >> PAGE_SHIFT);
894 mutex_enter(&uvm_pageqlock);
895 uvm_pagefree(pg);
896 mutex_exit(&uvm_pageqlock);
897 break;
899 default:
900 panic("%s: impossible", __func__);
903 if (by_list) {
904 TAILQ_REMOVE(&uobj->memq, &endmp, listq.queue);
906 mutex_exit(&uobj->vmobjlock);
907 return 0;
911 * uao_get: fetch me a page
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.
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.
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!!
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)
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);
943 UVMHIST_LOG(pdhist, "aobj=%p offset=%d, flags=%d",
944 (struct uvm_aobj *)uobj, offset, flags,0);
947 * get number of pages
950 maxpages = *npagesp;
953 * step 1: handled the case where fault data structures are locked.
956 if (flags & PGO_LOCKED) {
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).
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);
974 * if page is new, attempt to allocate the page,
975 * zero-fill'd.
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;
991 * to be useful must get a non-busy page
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;
1003 * useful page: busy/lock it and plug it in our
1004 * result array
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++;
1016 * step 1b: now we've either done everything needed or we
1017 * to unlock and do some waiting or I/O.
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;
1029 * step 2: get non-resident or busy pages.
1030 * object is locked. data structures are unlocked.
1033 if ((flags & PGO_SYNCIO) == 0) {
1034 goto done;
1037 for (lcv = 0, current_offset = offset ; lcv < maxpages ;
1038 lcv++, current_offset += PAGE_SIZE) {
1041 * - skip over pages we've already gotten or don't want
1042 * - skip over pages we don't _have_ to get
1045 if (pps[lcv] != NULL ||
1046 (lcv != centeridx && (flags & PGO_ALLPAGES) == 0))
1047 continue;
1049 pageidx = current_offset >> PAGE_SHIFT;
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.
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.
1067 /* top of "pps" while loop */
1068 while (pps[lcv] == NULL) {
1069 /* look for a resident page */
1070 ptmp = uvm_pagelookup(uobj, current_offset);
1072 /* not resident? allocate one now (if we can) */
1073 if (ptmp == NULL) {
1075 ptmp = uvm_pagealloc(uobj, current_offset,
1076 NULL, 0);
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;
1089 * safe with PQ's unlocked: because we just
1090 * alloc'd the page
1093 ptmp->pqflags |= PQ_AOBJ;
1096 * got new page ready for I/O. break pps while
1097 * loop. pps[lcv] is still NULL.
1100 break;
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;
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).
1122 /* we own it, caller must un-busy */
1123 ptmp->flags |= PG_BUSY;
1124 UVM_PAGE_OWN(ptmp, "uao_get2");
1125 pps[lcv] = ptmp;
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.
1133 if (pps[lcv])
1134 continue; /* next lcv */
1137 * we have a "fake/busy/clean" page that we just allocated.
1138 * do the needed "i/o", either reading from swap or zeroing.
1141 swslot = uao_find_swslot(&aobj->u_obj, pageidx);
1144 * just zero the page if there's nothing in swap.
1147 if (swslot == 0) {
1150 * page hasn't existed before, just zero it.
1153 uvm_pagezero(ptmp);
1154 } else {
1155 #if defined(VMSWAP)
1156 int error;
1158 UVMHIST_LOG(pdhist, "pagein from swslot %d",
1159 swslot, 0,0,0);
1162 * page in the swapped-out page.
1163 * unlock object for i/o, relock when done.
1166 mutex_exit(&uobj->vmobjlock);
1167 error = uvm_swap_get(ptmp, swslot, PGO_SYNCIO);
1168 mutex_enter(&uobj->vmobjlock);
1171 * I/O done. check for errors.
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);
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.
1187 swslot = uao_set_swslot(&aobj->u_obj, pageidx,
1188 SWSLOT_BAD);
1189 if (swslot > 0) {
1190 uvm_swap_markbad(swslot, 1);
1193 mutex_enter(&uvm_pageqlock);
1194 uvm_pagefree(ptmp);
1195 mutex_exit(&uvm_pageqlock);
1196 mutex_exit(&uobj->vmobjlock);
1197 return error;
1199 #else /* defined(VMSWAP) */
1200 panic("%s: pagein", __func__);
1201 #endif /* defined(VMSWAP) */
1204 if ((access_type & VM_PROT_WRITE) == 0) {
1205 ptmp->flags |= PG_CLEAN;
1206 pmap_clear_modify(ptmp);
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.
1214 * it is the callers job to:
1215 * => check if the page is released
1216 * => unbusy the page
1217 * => activate the page
1220 ptmp->flags &= ~PG_FAKE;
1221 pps[lcv] = ptmp;
1225 * finally, unlock object and return.
1228 done:
1229 mutex_exit(&uobj->vmobjlock);
1230 UVMHIST_LOG(pdhist, "<- done (OK)",0,0,0,0);
1231 return 0;
1234 #if defined(VMSWAP)
1237 * uao_dropswap: release any swap resources from this aobj page.
1239 * => aobj must be locked or have a reference count of 0.
1242 void
1243 uao_dropswap(struct uvm_object *uobj, int pageidx)
1245 int slot;
1247 slot = uao_set_swslot(uobj, pageidx, 0);
1248 if (slot) {
1249 uvm_swap_free(slot, 1);
1254 * page in every page in every aobj that is paged-out to a range of swslots.
1256 * => nothing should be locked.
1257 * => returns true if pagein was aborted due to lack of memory.
1260 bool
1261 uao_swap_off(int startslot, int endslot)
1263 struct uvm_aobj *aobj, *nextaobj;
1264 bool rv;
1267 * walk the list of all aobjs.
1270 restart:
1271 mutex_enter(&uao_list_lock);
1272 for (aobj = LIST_FIRST(&uao_list);
1273 aobj != NULL;
1274 aobj = nextaobj) {
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.
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;
1290 * add a ref to the aobj so it doesn't disappear
1291 * while we're working.
1294 uao_reference_locked(&aobj->u_obj);
1297 * now it's safe to unlock the uao list.
1300 mutex_exit(&uao_list_lock);
1303 * page in any pages in the swslot range.
1304 * if there's an error, abort and return the error.
1307 rv = uao_pagein(aobj, startslot, endslot);
1308 if (rv) {
1309 uao_detach_locked(&aobj->u_obj);
1310 return rv;
1314 * we're done with this aobj.
1315 * relock the list and drop our ref on the aobj.
1318 mutex_enter(&uao_list_lock);
1319 nextaobj = LIST_NEXT(aobj, u_list);
1320 uao_detach_locked(&aobj->u_obj);
1324 * done with traversal, unlock the list
1326 mutex_exit(&uao_list_lock);
1327 return false;
1332 * page in any pages from aobj in the given range.
1334 * => aobj must be locked and is returned locked.
1335 * => returns true if pagein was aborted due to lack of memory.
1337 static bool
1338 uao_pagein(struct uvm_aobj *aobj, int startslot, int endslot)
1340 bool rv;
1342 if (UAO_USES_SWHASH(aobj)) {
1343 struct uao_swhash_elt *elt;
1344 int buck;
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;
1353 for (i = 0; i < UAO_SWHASH_CLUSTER_SIZE; i++) {
1354 int slot = elt->slots[i];
1357 * if the slot isn't in range, skip it.
1360 if (slot < startslot ||
1361 slot >= endslot) {
1362 continue;
1366 * process the page,
1367 * the start over on this object
1368 * since the swhash elt
1369 * may have been freed.
1372 rv = uao_pagein_page(aobj,
1373 UAO_SWHASH_ELT_PAGEIDX_BASE(elt) + i);
1374 if (rv) {
1375 return rv;
1377 goto restart;
1381 } else {
1382 int i;
1384 for (i = 0; i < aobj->u_pages; i++) {
1385 int slot = aobj->u_swslots[i];
1388 * if the slot isn't in range, skip it
1391 if (slot < startslot || slot >= endslot) {
1392 continue;
1396 * process the page.
1399 rv = uao_pagein_page(aobj, i);
1400 if (rv) {
1401 return rv;
1406 return false;
1410 * page in a page from an aobj. used for swap_off.
1411 * returns true if pagein was aborted due to lack of memory.
1413 * => aobj must be locked and is returned locked.
1416 static bool
1417 uao_pagein_page(struct uvm_aobj *aobj, int pageidx)
1419 struct vm_page *pg;
1420 int rv, npages;
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 */
1430 * relock and finish up.
1433 mutex_enter(&aobj->u_obj.vmobjlock);
1434 switch (rv) {
1435 case 0:
1436 break;
1438 case EIO:
1439 case ERESTART:
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.
1447 return false;
1449 default:
1450 return true;
1454 * ok, we've got the page now.
1455 * mark it as dirty, clear its swslot and un-busy it.
1457 uao_dropswap(&aobj->u_obj, pageidx);
1460 * make sure it's on a page queue.
1462 mutex_enter(&uvm_pageqlock);
1463 if (pg->wire_count == 0)
1464 uvm_pageenqueue(pg);
1465 mutex_exit(&uvm_pageqlock);
1467 if (pg->flags & PG_WANTED) {
1468 wakeup(pg);
1470 pg->flags &= ~(PG_WANTED|PG_BUSY|PG_CLEAN|PG_FAKE);
1471 UVM_PAGE_OWN(pg, NULL);
1473 return false;
1477 * uao_dropswap_range: drop swapslots in the range.
1479 * => aobj must be locked and is returned locked.
1480 * => start is inclusive. end is exclusive.
1483 void
1484 uao_dropswap_range(struct uvm_object *uobj, voff_t start, voff_t end)
1486 struct uvm_aobj *aobj = (struct uvm_aobj *)uobj;
1488 KASSERT(mutex_owned(&uobj->vmobjlock));
1490 uao_dropswap_range1(aobj, start, end);
1493 static void
1494 uao_dropswap_range1(struct uvm_aobj *aobj, voff_t start, voff_t end)
1496 int swpgonlydelta = 0;
1498 if (end == 0) {
1499 end = INT64_MAX;
1502 if (UAO_USES_SWHASH(aobj)) {
1503 int i, hashbuckets = aobj->u_swhashmask + 1;
1504 voff_t taghi;
1505 voff_t taglo;
1507 taglo = UAO_SWHASH_ELT_TAG(start);
1508 taghi = UAO_SWHASH_ELT_TAG(end);
1510 for (i = 0; i < hashbuckets; i++) {
1511 struct uao_swhash_elt *elt, *next;
1513 for (elt = LIST_FIRST(&aobj->u_swhash[i]);
1514 elt != NULL;
1515 elt = next) {
1516 int startidx, endidx;
1517 int j;
1519 next = LIST_NEXT(elt, list);
1521 if (elt->tag < taglo || taghi < elt->tag) {
1522 continue;
1525 if (elt->tag == taglo) {
1526 startidx =
1527 UAO_SWHASH_ELT_PAGESLOT_IDX(start);
1528 } else {
1529 startidx = 0;
1532 if (elt->tag == taghi) {
1533 endidx =
1534 UAO_SWHASH_ELT_PAGESLOT_IDX(end);
1535 } else {
1536 endidx = UAO_SWHASH_CLUSTER_SIZE;
1539 for (j = startidx; j < endidx; j++) {
1540 int slot = elt->slots[j];
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--;
1554 if (elt->count == 0) {
1555 LIST_REMOVE(elt, list);
1556 pool_put(&uao_swhash_elt_pool, elt);
1560 } else {
1561 int i;
1563 if (aobj->u_pages < end) {
1564 end = aobj->u_pages;
1566 for (i = start; i < end; i++) {
1567 int slot = aobj->u_swslots[i];
1569 if (slot > 0) {
1570 uvm_swap_free(slot, 1);
1571 swpgonlydelta++;
1577 * adjust the counter of pages only in swap for all
1578 * the swap slots we've freed.
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);
1589 #endif /* defined(VMSWAP) */