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