Home | History | Annotate | Line # | Download | only in uvm
uvm_pager.c revision 1.13.2.5
      1 /*	$NetBSD: uvm_pager.c,v 1.13.2.5 1999/05/30 15:34:01 chs Exp $	*/
      2 
      3 /*
      4  * XXXCDC: "ROUGH DRAFT" QUALITY UVM PRE-RELEASE FILE!
      5  *	   >>>USE AT YOUR OWN RISK, WORK IS NOT FINISHED<<<
      6  */
      7 /*
      8  *
      9  * Copyright (c) 1997 Charles D. Cranor and Washington University.
     10  * All rights reserved.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *      This product includes software developed by Charles D. Cranor and
     23  *      Washington University.
     24  * 4. The name of the author may not be used to endorse or promote products
     25  *    derived from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     28  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     29  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     30  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     32  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     33  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     34  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     36  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     37  *
     38  * from: Id: uvm_pager.c,v 1.1.2.23 1998/02/02 20:38:06 chuck Exp
     39  */
     40 
     41 #include "opt_pmap_new.h"
     42 
     43 /*
     44  * uvm_pager.c: generic functions used to assist the pagers.
     45  */
     46 
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/proc.h>
     50 #include <sys/malloc.h>
     51 #include <sys/pool.h>
     52 
     53 #include <vm/vm.h>
     54 #include <vm/vm_page.h>
     55 #include <vm/vm_kern.h>
     56 
     57 #define UVM_PAGER
     58 #include <uvm/uvm.h>
     59 
     60 struct pool *uvm_aiobuf_pool;
     61 
     62 /*
     63  * list of uvm pagers in the system
     64  */
     65 
     66 extern struct uvm_pagerops aobj_pager;
     67 extern struct uvm_pagerops uvm_deviceops;
     68 extern struct uvm_pagerops uvm_vnodeops;
     69 #ifdef UBC
     70 extern struct uvm_pagerops ubc_pager;
     71 #endif
     72 
     73 struct uvm_pagerops *uvmpagerops[] = {
     74 	&aobj_pager,
     75 	&uvm_deviceops,
     76 	&uvm_vnodeops,
     77 #ifdef UBC
     78 	&ubc_pager,
     79 #endif
     80 };
     81 
     82 /*
     83  * the pager map: provides KVA for I/O
     84  */
     85 
     86 #define PAGER_MAP_SIZE       (4 * 1024 * 1024)
     87 vm_map_t pager_map;		/* XXX */
     88 simple_lock_data_t pager_map_wanted_lock;
     89 boolean_t pager_map_wanted;	/* locked by pager map */
     90 
     91 
     92 /*
     93  * uvm_pager_init: init pagers (at boot time)
     94  */
     95 
     96 void
     97 uvm_pager_init()
     98 {
     99 	int lcv;
    100 
    101 	/*
    102 	 * init pager map
    103 	 */
    104 
    105 	pager_map = uvm_km_suballoc(kernel_map, &uvm.pager_sva, &uvm.pager_eva,
    106 				    PAGER_MAP_SIZE, FALSE, FALSE, NULL);
    107 	simple_lock_init(&pager_map_wanted_lock);
    108 	pager_map_wanted = FALSE;
    109 
    110 	/*
    111 	 * init ASYNC I/O queue
    112 	 */
    113 
    114 	TAILQ_INIT(&uvm.aio_done);
    115 	uvm_aiobuf_pool = pool_create(sizeof(struct uvm_aiobuf),
    116 				      0, 0, 0, "aiobuf", 0, NULL, NULL, 0);
    117 
    118 
    119 	/*
    120 	 * call pager init functions
    121 	 */
    122 	for (lcv = 0 ; lcv < sizeof(uvmpagerops)/sizeof(struct uvm_pagerops *);
    123 	    lcv++) {
    124 		if (uvmpagerops[lcv]->pgo_init)
    125 			uvmpagerops[lcv]->pgo_init();
    126 	}
    127 }
    128 
    129 /*
    130  * uvm_pagermapin: map pages into KVA (pager_map) for I/O that needs mappings
    131  *
    132  * we basically just map in a blank map entry to reserve the space in the
    133  * map and then use pmap_enter() to put the mappings in by hand.
    134  */
    135 
    136 vaddr_t
    137 uvm_pagermapin(pps, npages, aiop, waitf)
    138 	struct vm_page **pps;
    139 	int npages;
    140 	struct uvm_aiodesc **aiop;	/* OUT */
    141 	int waitf;
    142 {
    143 	vsize_t size;
    144 	vaddr_t kva;
    145 	struct uvm_aiodesc *aio;
    146 #if !defined(PMAP_NEW)
    147 	vaddr_t cva;
    148 	struct vm_page *pp;
    149 #endif
    150 	UVMHIST_FUNC("uvm_pagermapin"); UVMHIST_CALLED(maphist);
    151 
    152 	UVMHIST_LOG(maphist,"(pps=0x%x, npages=%d, aiop=0x%x, waitf=%d)",
    153 	      pps, npages, aiop, waitf);
    154 
    155 ReStart:
    156 	if (aiop) {
    157 		MALLOC(aio, struct uvm_aiodesc *, sizeof(*aio), M_TEMP, waitf);
    158 		if (aio == NULL)
    159 			return(0);
    160 		*aiop = aio;
    161 	} else {
    162 		aio = NULL;
    163 	}
    164 
    165 	size = npages << PAGE_SHIFT;
    166 	kva = NULL;			/* let system choose VA */
    167 
    168 	if (uvm_map(pager_map, &kva, size, NULL,
    169 	      UVM_UNKNOWN_OFFSET, UVM_FLAG_NOMERGE) != KERN_SUCCESS) {
    170 		if (waitf == M_NOWAIT) {
    171 			if (aio)
    172 				FREE(aio, M_TEMP);
    173 			UVMHIST_LOG(maphist,"<- NOWAIT failed", 0,0,0,0);
    174 			return(NULL);
    175 		}
    176 		simple_lock(&pager_map_wanted_lock);
    177 		pager_map_wanted = TRUE;
    178 		UVMHIST_LOG(maphist, "  SLEEPING on pager_map",0,0,0,0);
    179 		UVM_UNLOCK_AND_WAIT(pager_map, &pager_map_wanted_lock, FALSE,
    180 		    "pager_map",0);
    181 		goto ReStart;
    182 	}
    183 
    184 #if defined(PMAP_NEW)
    185 	/*
    186 	 * XXX: (ab)using the pmap module to store state info for us.
    187 	 * (pmap stores the PAs... we fetch them back later and convert back
    188 	 * to pages with PHYS_TO_VM_PAGE).
    189 	 */
    190 	pmap_kenter_pgs(kva, pps, npages);
    191 
    192 #else /* PMAP_NEW */
    193 
    194 	/* got it */
    195 	for (cva = kva ; size != 0 ; size -= PAGE_SIZE, cva += PAGE_SIZE) {
    196 		pp = *pps++;
    197 #ifdef DEBUG
    198 		if ((pp->flags & PG_BUSY) == 0)
    199 			panic("uvm_pagermapin: page not busy");
    200 #endif
    201 
    202 		pmap_enter(vm_map_pmap(pager_map), cva, VM_PAGE_TO_PHYS(pp),
    203 		    VM_PROT_DEFAULT, TRUE);
    204 	}
    205 
    206 #endif /* PMAP_NEW */
    207 
    208 	UVMHIST_LOG(maphist, "<- done (KVA=0x%x)", kva,0,0,0);
    209 	return(kva);
    210 }
    211 
    212 /*
    213  * uvm_pagermapout: remove pager_map mapping
    214  *
    215  * we remove our mappings by hand and then remove the mapping (waking
    216  * up anyone wanting space).
    217  */
    218 
    219 void
    220 uvm_pagermapout(kva, npages)
    221 	vaddr_t kva;
    222 	int npages;
    223 {
    224 	vsize_t size = npages << PAGE_SHIFT;
    225 	vm_map_entry_t entries;
    226 	UVMHIST_FUNC("uvm_pagermapout"); UVMHIST_CALLED(maphist);
    227 
    228 	UVMHIST_LOG(maphist, " (kva=0x%x, npages=%d)", kva, npages,0,0);
    229 
    230 	/*
    231 	 * duplicate uvm_unmap, but add in pager_map_wanted handling.
    232 	 */
    233 
    234 	vm_map_lock(pager_map);
    235 	(void) uvm_unmap_remove(pager_map, kva, kva + size, &entries);
    236 	simple_lock(&pager_map_wanted_lock);
    237 	if (pager_map_wanted) {
    238 		pager_map_wanted = FALSE;
    239 		wakeup(pager_map);
    240 	}
    241 	simple_unlock(&pager_map_wanted_lock);
    242 	vm_map_unlock(pager_map);
    243 	if (entries)
    244 		uvm_unmap_detach(entries, 0);
    245 
    246 	UVMHIST_LOG(maphist,"<- done",0,0,0,0);
    247 }
    248 
    249 /*
    250  * uvm_mk_pcluster
    251  *
    252  * generic "make 'pager put' cluster" function.  a pager can either
    253  * [1] set pgo_mk_pcluster to NULL (never cluster), [2] set it to this
    254  * generic function, or [3] set it to a pager specific function.
    255  *
    256  * => caller must lock object _and_ pagequeues (since we need to look
    257  *    at active vs. inactive bits, etc.)
    258  * => caller must make center page busy and write-protect it
    259  * => we mark all cluster pages busy for the caller
    260  * => the caller must unbusy all pages (and check wanted/released
    261  *    status if it drops the object lock)
    262  * => flags:
    263  *      PGO_ALLPAGES:  all pages in object are valid targets
    264  *      !PGO_ALLPAGES: use "lo" and "hi" to limit range of cluster
    265  *      PGO_DOACTCLUST: include active pages in cluster.
    266  *        NOTE: the caller should clear PG_CLEANCHK bits if PGO_DOACTCLUST.
    267  *              PG_CLEANCHK is only a hint, but clearing will help reduce
    268  *		the number of calls we make to the pmap layer.
    269  */
    270 
    271 struct vm_page **
    272 uvm_mk_pcluster(uobj, pps, npages, center, flags, mlo, mhi)
    273 	struct uvm_object *uobj;	/* IN */
    274 	struct vm_page **pps, *center;  /* IN/OUT, IN */
    275 	int *npages, flags;		/* IN/OUT, IN */
    276 	vaddr_t mlo, mhi;		/* IN (if !PGO_ALLPAGES) */
    277 {
    278 	struct vm_page **ppsp, *pclust;
    279 	vaddr_t lo, hi, curoff;
    280 	int center_idx, forward, incr;
    281 	UVMHIST_FUNC("uvm_mk_pcluster"); UVMHIST_CALLED(maphist);
    282 
    283 	/*
    284 	 * center page should already be busy and write protected.  XXX:
    285 	 * suppose page is wired?  if we lock, then a process could
    286 	 * fault/block on it.  if we don't lock, a process could write the
    287 	 * pages in the middle of an I/O.  (consider an msync()).  let's
    288 	 * lock it for now (better to delay than corrupt data?).
    289 	 */
    290 
    291 	/*
    292 	 * get cluster boundaries, check sanity, and apply our limits as well.
    293 	 */
    294 
    295 	uobj->pgops->pgo_cluster(uobj, center->offset, &lo, &hi);
    296 	if ((flags & PGO_ALLPAGES) == 0) {
    297 		if (lo < mlo)
    298 			lo = mlo;
    299 		if (hi > mhi)
    300 			hi = mhi;
    301 	}
    302 	if ((hi - lo) >> PAGE_SHIFT > *npages) { /* pps too small, bail out! */
    303 #ifdef DIAGNOSTIC
    304 	    printf("uvm_mk_pcluster: provided page array too small (fixed)\n");
    305 #endif
    306 		pps[0] = center;
    307 		*npages = 1;
    308 		return(pps);
    309 	}
    310 
    311 	/*
    312 	 * now determine the center and attempt to cluster around the
    313 	 * edges
    314 	 */
    315 
    316 	center_idx = (center->offset - lo) >> PAGE_SHIFT;
    317 	pps[center_idx] = center;	/* plug in the center page */
    318 	ppsp = &pps[center_idx];
    319 	*npages = 1;
    320 
    321 	/*
    322 	 * attempt to cluster around the left [backward], and then
    323 	 * the right side [forward].
    324 	 *
    325 	 * note that for inactive pages (pages that have been deactivated)
    326 	 * there are no valid mappings and PG_CLEAN should be up to date.
    327 	 * [i.e. there is no need to query the pmap with pmap_is_modified
    328 	 * since there are no mappings].
    329 	 */
    330 
    331 	for (forward  = 0 ; forward <= 1 ; forward++) {
    332 		incr = forward ? PAGE_SIZE : -PAGE_SIZE;
    333 		curoff = center->offset + incr;
    334 		for ( ;(forward == 0 && curoff >= lo) ||
    335 		       (forward && curoff < hi);
    336 		      curoff += incr) {
    337 
    338 			pclust = uvm_pagelookup(uobj, curoff); /* lookup page */
    339 			if (pclust == NULL) {
    340 				break;			/* no page */
    341 			}
    342 			/* handle active pages */
    343 			/* NOTE: inactive pages don't have pmap mappings */
    344 			if ((pclust->pqflags & PQ_INACTIVE) == 0) {
    345 				if ((flags & PGO_DOACTCLUST) == 0) {
    346 					/* dont want mapped pages at all */
    347 					break;
    348 				}
    349 
    350 				/* make sure "clean" bit is sync'd */
    351 				if ((pclust->flags & PG_CLEANCHK) == 0) {
    352 					if ((pclust->flags & (PG_CLEAN|PG_BUSY))
    353 					   == PG_CLEAN &&
    354 					   pmap_is_modified(PMAP_PGARG(pclust)))
    355 					pclust->flags &= ~PG_CLEAN;
    356 					/* now checked */
    357 					pclust->flags |= PG_CLEANCHK;
    358 				}
    359 			}
    360 
    361 			/* is page available for cleaning and does it need it */
    362 			if ((pclust->flags & (PG_CLEAN|PG_BUSY)) != 0) {
    363 				break;	/* page is already clean or is busy */
    364 			}
    365 
    366 #ifdef UBC
    367 			/* XXX for now, disable putpage clustering */
    368 			break;
    369 #endif
    370 
    371 			/* yes!   enroll the page in our array */
    372 			pclust->flags |= PG_BUSY;		/* busy! */
    373 			UVM_PAGE_OWN(pclust, "uvm_mk_pcluster");
    374 
    375 			/* XXX: protect wired page?   see above comment. */
    376 			pmap_page_protect(PMAP_PGARG(pclust), VM_PROT_READ);
    377 			if (!forward) {
    378 				ppsp--;			/* back up one page */
    379 				*ppsp = pclust;
    380 			} else {
    381 				/* move forward one page */
    382 				ppsp[*npages] = pclust;
    383 			}
    384 			(*npages)++;
    385 		}
    386 	}
    387 
    388 	/*
    389 	 * done!  return the cluster array to the caller!!!
    390 	 */
    391 
    392 	UVMHIST_LOG(maphist, "<- done",0,0,0,0);
    393 	return(ppsp);
    394 }
    395 
    396 
    397 /*
    398  * uvm_shareprot: generic share protect routine
    399  *
    400  * => caller must lock map entry's map
    401  * => caller must lock object pointed to by map entry
    402  */
    403 
    404 void
    405 uvm_shareprot(entry, prot)
    406 	vm_map_entry_t entry;
    407 	vm_prot_t prot;
    408 {
    409 	struct uvm_object *uobj = entry->object.uvm_obj;
    410 	struct vm_page *pp;
    411 	vaddr_t start, stop;
    412 	UVMHIST_FUNC("uvm_shareprot"); UVMHIST_CALLED(maphist);
    413 
    414 	if (UVM_ET_ISSUBMAP(entry))
    415 		panic("uvm_shareprot: non-object attached");
    416 
    417 	start = entry->offset;
    418 	stop = start + (entry->end - entry->start);
    419 
    420 	/*
    421 	 * traverse list of pages in object.   if page in range, pmap_prot it
    422 	 */
    423 
    424 	for (pp = uobj->memq.tqh_first ; pp != NULL ; pp = pp->listq.tqe_next) {
    425 		if (pp->offset >= start && pp->offset < stop)
    426 			pmap_page_protect(PMAP_PGARG(pp), prot);
    427 	}
    428 	UVMHIST_LOG(maphist, "<- done",0,0,0,0);
    429 }
    430 
    431 /*
    432  * uvm_pager_put: high level pageout routine
    433  *
    434  * we want to pageout page "pg" to backing store, clustering if
    435  * possible.
    436  *
    437  * => page queues must be locked by caller
    438  * => if page is not swap-backed, then "uobj" points to the object
    439  *	backing it.   this object should be locked by the caller.
    440  * => if page is swap-backed, then "uobj" should be NULL.
    441  * => "pg" should be PG_BUSY (by caller), and !PG_CLEAN
    442  *    for swap-backed memory, "pg" can be NULL if there is no page
    443  *    of interest [sometimes the case for the pagedaemon]
    444  * => "ppsp_ptr" should point to an array of npages vm_page pointers
    445  *	for possible cluster building
    446  * => flags (first two for non-swap-backed pages)
    447  *	PGO_ALLPAGES: all pages in uobj are valid targets
    448  *	PGO_DOACTCLUST: include "PQ_ACTIVE" pages as valid targets
    449  *	PGO_SYNCIO: do SYNC I/O (no async)
    450  *	PGO_PDFREECLUST: pagedaemon: drop cluster on successful I/O
    451  * => start/stop: if (uobj && !PGO_ALLPAGES) limit targets to this range
    452  *		  if (!uobj) start is the (daddr_t) of the starting swapblk
    453  * => return state:
    454  *	1. we return the VM_PAGER status code of the pageout
    455  *	2. we return with the page queues unlocked
    456  *	3. if (uobj != NULL) [!swap_backed] we return with
    457  *		uobj locked _only_ if PGO_PDFREECLUST is set
    458  *		AND result != VM_PAGER_PEND.   in all other cases
    459  *		we return with uobj unlocked.   [this is a hack
    460  *		that allows the pagedaemon to save one lock/unlock
    461  *		pair in the !swap_backed case since we have to
    462  *		lock the uobj to drop the cluster anyway]
    463  *	4. on errors we always drop the cluster.   thus, if we return
    464  *		!PEND, !OK, then the caller only has to worry about
    465  *		un-busying the main page (not the cluster pages).
    466  *	5. on success, if !PGO_PDFREECLUST, we return the cluster
    467  *		with all pages busy (caller must un-busy and check
    468  *		wanted/released flags).
    469  */
    470 
    471 int
    472 uvm_pager_put(uobj, pg, ppsp_ptr, npages, flags, start, stop)
    473 	struct uvm_object *uobj;	/* IN */
    474 	struct vm_page *pg, ***ppsp_ptr;/* IN, IN/OUT */
    475 	int *npages;			/* IN/OUT */
    476 	int flags;			/* IN */
    477 	vaddr_t start, stop;		/* IN, IN */
    478 {
    479 	int result;
    480 	daddr_t swblk;
    481 	struct vm_page **ppsp = *ppsp_ptr;
    482 
    483 	/*
    484 	 * note that uobj is null  if we are doing a swap-backed pageout.
    485 	 * note that uobj is !null if we are doing normal object pageout.
    486 	 * note that the page queues must be locked to cluster.
    487 	 */
    488 
    489 	if (uobj) {	/* if !swap-backed */
    490 
    491 		/*
    492 		 * attempt to build a cluster for pageout using its
    493 		 * make-put-cluster function (if it has one).
    494 		 */
    495 
    496 		if (uobj->pgops->pgo_mk_pcluster) {
    497 			ppsp = uobj->pgops->pgo_mk_pcluster(uobj, ppsp,
    498 			    npages, pg, flags, start, stop);
    499 			*ppsp_ptr = ppsp;  /* update caller's pointer */
    500 		} else {
    501 			ppsp[0] = pg;
    502 			*npages = 1;
    503 		}
    504 
    505 		swblk = 0;		/* XXX: keep gcc happy */
    506 
    507 	} else {
    508 
    509 		/*
    510 		 * for swap-backed pageout, the caller (the pagedaemon) has
    511 		 * already built the cluster for us.   the starting swap
    512 		 * block we are writing to has been passed in as "start."
    513 		 * "pg" could be NULL if there is no page we are especially
    514 		 * interested in (in which case the whole cluster gets dropped
    515 		 * in the event of an error or a sync "done").
    516 		 */
    517 		swblk = (daddr_t) start;
    518 		/* ppsp and npages should be ok */
    519 	}
    520 
    521 	/* now that we've clustered we can unlock the page queues */
    522 	uvm_unlock_pageq();
    523 
    524 	/*
    525 	 * now attempt the I/O.   if we have a failure and we are
    526 	 * clustered, we will drop the cluster and try again.
    527 	 */
    528 
    529 ReTry:
    530 	if (uobj) {
    531 		/* object is locked */
    532 		simple_lock_assert(&uobj->vmobjlock, SLOCK_LOCKED);
    533 		result = uobj->pgops->pgo_put(uobj, ppsp, *npages, flags);
    534 		/* object is now unlocked */
    535 		simple_lock_assert(&uobj->vmobjlock, SLOCK_UNLOCKED);
    536 	} else {
    537 		/* nothing locked */
    538 		result = uvm_swap_put(swblk, ppsp, *npages, flags);
    539 		/* nothing locked */
    540 	}
    541 
    542 	/*
    543 	 * we have attempted the I/O.
    544 	 *
    545 	 * if the I/O was a success then:
    546 	 * 	if !PGO_PDFREECLUST, we return the cluster to the
    547 	 *		caller (who must un-busy all pages)
    548 	 *	else we un-busy cluster pages for the pagedaemon
    549 	 *
    550 	 * if I/O is pending (async i/o) then we return the pending code.
    551 	 * [in this case the async i/o done function must clean up when
    552 	 *  i/o is done...]
    553 	 */
    554 
    555 	if (result == VM_PAGER_PEND || result == VM_PAGER_OK) {
    556 		if (result == VM_PAGER_OK && (flags & PGO_PDFREECLUST)) {
    557 			/*
    558 			 * drop cluster and relock object (only if I/O is
    559 			 * not pending)
    560 			 */
    561 			if (uobj)
    562 				/* required for dropcluster */
    563 				simple_lock(&uobj->vmobjlock);
    564 			if (*npages > 1 || pg == NULL)
    565 				uvm_pager_dropcluster(uobj, pg, ppsp, npages,
    566 				    PGO_PDFREECLUST, 0);
    567 			/* if (uobj): object still locked, as per
    568 			 * return-state item #3 */
    569 		}
    570 		return (result);
    571 	}
    572 
    573 	/*
    574 	 * a pager error occured.    if we have clustered, we drop the
    575 	 * cluster and try again.
    576 	 */
    577 
    578 	if (*npages > 1 || pg == NULL) {
    579 		if (uobj)
    580 			simple_lock(&uobj->vmobjlock);
    581 		uvm_pager_dropcluster(uobj, pg, ppsp, npages, PGO_REALLOCSWAP,
    582 		    swblk);
    583 		if (pg != NULL)
    584 			goto ReTry;
    585 	}
    586 
    587 	/*
    588 	 * a pager error occured (even after dropping the cluster, if there
    589 	 * was one).    give up!   the caller only has one page ("pg")
    590 	 * to worry about.
    591 	 */
    592 
    593 	if (uobj && (flags & PGO_PDFREECLUST) != 0)
    594 		simple_lock(&uobj->vmobjlock);
    595 	return(result);
    596 }
    597 
    598 /*
    599  * uvm_pager_dropcluster: drop a cluster we have built (because we
    600  * got an error, or, if PGO_PDFREECLUST we are un-busying the
    601  * cluster pages on behalf of the pagedaemon).
    602  *
    603  * => uobj, if non-null, is a non-swap-backed object that is
    604  *	locked by the caller.   we return with this object still
    605  *	locked.
    606  * => page queues are not locked
    607  * => pg is our page of interest (the one we clustered around, can be null)
    608  * => ppsp/npages is our current cluster
    609  * => flags: PGO_PDFREECLUST: pageout was a success: un-busy cluster
    610  *	pages on behalf of the pagedaemon.
    611  *           PGO_REALLOCSWAP: drop previously allocated swap slots for
    612  *		clustered swap-backed pages (except for "pg" if !NULL)
    613  *		"swblk" is the start of swap alloc (e.g. for ppsp[0])
    614  *		[only meaningful if swap-backed (uobj == NULL)]
    615  */
    616 
    617 
    618 void
    619 uvm_pager_dropcluster(uobj, pg, ppsp, npages, flags, swblk)
    620 	struct uvm_object *uobj;	/* IN */
    621 	struct vm_page *pg, **ppsp;	/* IN, IN/OUT */
    622 	int *npages;			/* IN/OUT */
    623 	int flags;
    624 	int swblk;			/* valid if (uobj == NULL &&
    625 					   PGO_REALLOCSWAP) */
    626 {
    627 	int lcv;
    628 	boolean_t obj_is_alive;
    629 	struct uvm_object *saved_uobj;
    630 
    631 	/*
    632 	 * if we need to reallocate swap space for the cluster we are dropping
    633 	 * (true if swap-backed and PGO_REALLOCSWAP) then free the old
    634 	 * allocation now.   save a block for "pg" if it is non-NULL.
    635 	 *
    636 	 * note that we will zap the object's pointer to swap in the "for" loop
    637 	 * below...
    638 	 */
    639 
    640 	if (uobj == NULL && (flags & PGO_REALLOCSWAP)) {
    641 		if (pg)
    642 			uvm_swap_free(swblk + 1, *npages - 1);
    643 		else
    644 			uvm_swap_free(swblk, *npages);
    645 	}
    646 
    647 	/*
    648 	 * drop all pages but "pg"
    649 	 */
    650 
    651 	for (lcv = 0 ; lcv < *npages ; lcv++) {
    652 
    653 		/* skip "pg" or empty slot */
    654 		if (ppsp[lcv] == pg || ppsp[lcv] == NULL)
    655 			continue;
    656 
    657 		/*
    658 		 * if swap-backed, gain lock on object that owns page.  note
    659 		 * that PQ_ANON bit can't change as long as we are holding
    660 		 * the PG_BUSY bit (so there is no need to lock the page
    661 		 * queues to test it).
    662 		 *
    663 		 * once we have the lock, dispose of the pointer to swap, if
    664 		 * requested
    665 		 */
    666 		if (!uobj) {
    667 			if (ppsp[lcv]->pqflags & PQ_ANON) {
    668 				simple_lock(&ppsp[lcv]->uanon->an_lock);
    669 				if (flags & PGO_REALLOCSWAP)
    670 					  /* zap swap block */
    671 					  ppsp[lcv]->uanon->an_swslot = 0;
    672 			} else {
    673 				simple_lock(&ppsp[lcv]->uobject->vmobjlock);
    674 				if (flags & PGO_REALLOCSWAP)
    675 					uao_set_swslot(ppsp[lcv]->uobject,
    676 					    ppsp[lcv]->offset >> PAGE_SHIFT, 0);
    677 			}
    678 		}
    679 
    680 		/* did someone want the page while we had it busy-locked? */
    681 		if (ppsp[lcv]->flags & PG_WANTED) {
    682 			/* still holding obj lock */
    683 			wakeup(ppsp[lcv]);
    684 		}
    685 
    686 		/* if page was released, release it.  otherwise un-busy it */
    687 		if (ppsp[lcv]->flags & PG_RELEASED) {
    688 
    689 			if (ppsp[lcv]->pqflags & PQ_ANON) {
    690 				/* so that anfree will free */
    691 				ppsp[lcv]->flags &= ~(PG_BUSY);
    692 				UVM_PAGE_OWN(ppsp[lcv], NULL);
    693 
    694 				pmap_page_protect(PMAP_PGARG(ppsp[lcv]),
    695 				    VM_PROT_NONE); /* be safe */
    696 				simple_unlock(&ppsp[lcv]->uanon->an_lock);
    697 				/* kills anon and frees pg */
    698 				uvm_anfree(ppsp[lcv]->uanon);
    699 
    700 				continue;
    701 			}
    702 
    703 			/*
    704 			 * pgo_releasepg will dump the page for us
    705 			 */
    706 
    707 #ifdef DIAGNOSTIC
    708 			if (ppsp[lcv]->uobject->pgops->pgo_releasepg == NULL)
    709 				panic("uvm_pager_dropcluster: no releasepg "
    710 				    "function");
    711 #endif
    712 			saved_uobj = ppsp[lcv]->uobject;
    713 			obj_is_alive =
    714 			    saved_uobj->pgops->pgo_releasepg(ppsp[lcv], NULL);
    715 
    716 #ifdef DIAGNOSTIC
    717 			/* for normal objects, "pg" is still PG_BUSY by us,
    718 			 * so obj can't die */
    719 			if (uobj && !obj_is_alive)
    720 				panic("uvm_pager_dropcluster: object died "
    721 				    "with active page");
    722 #endif
    723 			/* only unlock the object if it is still alive...  */
    724 			if (obj_is_alive && saved_uobj != uobj)
    725 				simple_unlock(&saved_uobj->vmobjlock);
    726 
    727 			/*
    728 			 * XXXCDC: suppose uobj died in the pgo_releasepg?
    729 			 * how pass that
    730 			 * info up to caller.  we are currently ignoring it...
    731 			 */
    732 
    733 			continue;		/* next page */
    734 
    735 		} else {
    736 			ppsp[lcv]->flags &= ~(PG_BUSY|PG_WANTED|PG_FAKE);
    737 			UVM_PAGE_OWN(ppsp[lcv], NULL);
    738 		}
    739 
    740 		/*
    741 		 * if we are operating on behalf of the pagedaemon and we
    742 		 * had a successful pageout update the page!
    743 		 */
    744 		if (flags & PGO_PDFREECLUST) {
    745 			/* XXX: with PMAP_NEW ref should already be clear,
    746 			 * but don't trust! */
    747 			pmap_clear_reference(PMAP_PGARG(ppsp[lcv]));
    748 			pmap_clear_modify(PMAP_PGARG(ppsp[lcv]));
    749 			ppsp[lcv]->flags |= PG_CLEAN;
    750 		}
    751 
    752 		/* if anonymous cluster, unlock object and move on */
    753 		if (!uobj) {
    754 			if (ppsp[lcv]->pqflags & PQ_ANON)
    755 				simple_unlock(&ppsp[lcv]->uanon->an_lock);
    756 			else
    757 				simple_unlock(&ppsp[lcv]->uobject->vmobjlock);
    758 		}
    759 
    760 	}
    761 
    762 	/*
    763 	 * drop to a cluster of 1 page ("pg") if requested
    764 	 */
    765 
    766 	if (pg && (flags & PGO_PDFREECLUST) == 0) {
    767 		/*
    768 		 * if we are not a successful pageout, we make a 1 page cluster.
    769 		 */
    770 		ppsp[0] = pg;
    771 		*npages = 1;
    772 
    773 		/*
    774 		 * assign new swap block to new cluster, if anon backed
    775 		 */
    776 		if (uobj == NULL && (flags & PGO_REALLOCSWAP)) {
    777 			if (pg->pqflags & PQ_ANON) {
    778 				simple_lock(&pg->uanon->an_lock);
    779 				pg->uanon->an_swslot = swblk;	/* reassign */
    780 				simple_unlock(&pg->uanon->an_lock);
    781 			} else {
    782 				simple_lock(&pg->uobject->vmobjlock);
    783 				uao_set_swslot(pg->uobject,
    784 				    pg->offset >> PAGE_SHIFT, swblk);
    785 				simple_unlock(&pg->uobject->vmobjlock);
    786 			}
    787 		}
    788 	}
    789 }
    790 
    791 void
    792 uvm_aio_biodone(bp)
    793 	struct buf *bp;
    794 {
    795 	struct uvm_aiobuf *abp = (void *)bp;
    796 	int s;
    797 
    798 	s = splbio();
    799 	simple_lock(&uvm.aiodoned_lock);	/* locks uvm.aio_done */
    800 	TAILQ_INSERT_TAIL(&uvm.aio_done, &abp->aio, aioq);
    801 	wakeup(&uvm.aiodoned);
    802 	simple_unlock(&uvm.aiodoned_lock);
    803 	splx(s);
    804 }
    805 
    806 void
    807 uvm_aio_aiodone(aio)
    808 	struct uvm_aiodesc *aio;
    809 {
    810 	struct uvm_aiobuf *abp = aio->pd_ptr;
    811 	struct vm_page *pgs[aio->npages];
    812 	int s, i;
    813 	boolean_t release;
    814 
    815 	release = (abp->buf.b_flags & (B_ERROR|B_READ)) == (B_ERROR|B_READ);
    816 	for (i = 0; i < aio->npages; i++) {
    817 		pgs[i] = uvm_pageratop(aio->kva + (i << PAGE_SHIFT));
    818 
    819 		/*
    820 		 * if this is an async read and we got an error,
    821 		 * mark the pages PG_RELEASED so that uvm_pager_dropcluster()
    822 		 * will free them.
    823 		 */
    824 
    825 		if (release) {
    826 			pgs[i]->flags |= PG_RELEASED;
    827 		}
    828 	}
    829 	uvm_pagermapout(aio->kva, aio->npages);
    830 	uvm_pager_dropcluster((struct uvm_object *)abp->buf.b_vp, NULL, pgs,
    831 			      &aio->npages, PGO_PDFREECLUST, 0);
    832 
    833 	s = splbio();
    834 	pool_put(uvm_aiobuf_pool, abp);
    835 	splx(s);
    836 }
    837