Home | History | Annotate | Line # | Download | only in uvm
uvm_pager.c revision 1.16
      1 /*	$NetBSD: uvm_pager.c,v 1.16 1999/03/26 21:58:39 mycroft 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 #if !defined(PMAP_NEW)
    132 	vaddr_t cva;
    133 	struct vm_page *pp;
    134 #endif
    135 	UVMHIST_FUNC("uvm_pagermapin"); UVMHIST_CALLED(maphist);
    136 
    137 	UVMHIST_LOG(maphist,"(pps=0x%x, npages=%d, aiop=0x%x, waitf=%d)",
    138 	      pps, npages, aiop, waitf);
    139 
    140 ReStart:
    141 	if (aiop) {
    142 		MALLOC(aio, struct uvm_aiodesc *, sizeof(*aio), M_TEMP, waitf);
    143 		if (aio == NULL)
    144 			return(0);
    145 		*aiop = aio;
    146 	} else {
    147 		aio = NULL;
    148 	}
    149 
    150 	size = npages << PAGE_SHIFT;
    151 	kva = NULL;			/* let system choose VA */
    152 
    153 	if (uvm_map(pager_map, &kva, size, NULL,
    154 	      UVM_UNKNOWN_OFFSET, UVM_FLAG_NOMERGE) != KERN_SUCCESS) {
    155 		if (waitf == M_NOWAIT) {
    156 			if (aio)
    157 				FREE(aio, M_TEMP);
    158 			UVMHIST_LOG(maphist,"<- NOWAIT failed", 0,0,0,0);
    159 			return(NULL);
    160 		}
    161 		simple_lock(&pager_map_wanted_lock);
    162 		pager_map_wanted = TRUE;
    163 		UVMHIST_LOG(maphist, "  SLEEPING on pager_map",0,0,0,0);
    164 		UVM_UNLOCK_AND_WAIT(pager_map, &pager_map_wanted_lock, FALSE,
    165 		    "pager_map",0);
    166 		goto ReStart;
    167 	}
    168 
    169 #if defined(PMAP_NEW)
    170 	/*
    171 	 * XXX: (ab)using the pmap module to store state info for us.
    172 	 * (pmap stores the PAs... we fetch them back later and convert back
    173 	 * to pages with PHYS_TO_VM_PAGE).
    174 	 */
    175 	pmap_kenter_pgs(kva, pps, npages);
    176 
    177 #else /* PMAP_NEW */
    178 
    179 	/* got it */
    180 	for (cva = kva ; size != 0 ; size -= PAGE_SIZE, cva += PAGE_SIZE) {
    181 		pp = *pps++;
    182 #ifdef DEBUG
    183 		if ((pp->flags & PG_BUSY) == 0)
    184 			panic("uvm_pagermapin: page not busy");
    185 #endif
    186 
    187 		pmap_enter(vm_map_pmap(pager_map), cva, VM_PAGE_TO_PHYS(pp),
    188 		    VM_PROT_DEFAULT, TRUE, 0);
    189 	}
    190 
    191 #endif /* PMAP_NEW */
    192 
    193 	UVMHIST_LOG(maphist, "<- done (KVA=0x%x)", kva,0,0,0);
    194 	return(kva);
    195 }
    196 
    197 /*
    198  * uvm_pagermapout: remove pager_map mapping
    199  *
    200  * we remove our mappings by hand and then remove the mapping (waking
    201  * up anyone wanting space).
    202  */
    203 
    204 void
    205 uvm_pagermapout(kva, npages)
    206 	vaddr_t kva;
    207 	int npages;
    208 {
    209 	vsize_t size = npages << PAGE_SHIFT;
    210 	vm_map_entry_t entries;
    211 	UVMHIST_FUNC("uvm_pagermapout"); UVMHIST_CALLED(maphist);
    212 
    213 	UVMHIST_LOG(maphist, " (kva=0x%x, npages=%d)", kva, npages,0,0);
    214 
    215 	/*
    216 	 * duplicate uvm_unmap, but add in pager_map_wanted handling.
    217 	 */
    218 
    219 	vm_map_lock(pager_map);
    220 	(void) uvm_unmap_remove(pager_map, kva, kva + size, &entries);
    221 	simple_lock(&pager_map_wanted_lock);
    222 	if (pager_map_wanted) {
    223 		pager_map_wanted = FALSE;
    224 		wakeup(pager_map);
    225 	}
    226 	simple_unlock(&pager_map_wanted_lock);
    227 	vm_map_unlock(pager_map);
    228 	if (entries)
    229 		uvm_unmap_detach(entries, 0);
    230 
    231 	UVMHIST_LOG(maphist,"<- done",0,0,0,0);
    232 }
    233 
    234 /*
    235  * uvm_mk_pcluster
    236  *
    237  * generic "make 'pager put' cluster" function.  a pager can either
    238  * [1] set pgo_mk_pcluster to NULL (never cluster), [2] set it to this
    239  * generic function, or [3] set it to a pager specific function.
    240  *
    241  * => caller must lock object _and_ pagequeues (since we need to look
    242  *    at active vs. inactive bits, etc.)
    243  * => caller must make center page busy and write-protect it
    244  * => we mark all cluster pages busy for the caller
    245  * => the caller must unbusy all pages (and check wanted/released
    246  *    status if it drops the object lock)
    247  * => flags:
    248  *      PGO_ALLPAGES:  all pages in object are valid targets
    249  *      !PGO_ALLPAGES: use "lo" and "hi" to limit range of cluster
    250  *      PGO_DOACTCLUST: include active pages in cluster.
    251  *        NOTE: the caller should clear PG_CLEANCHK bits if PGO_DOACTCLUST.
    252  *              PG_CLEANCHK is only a hint, but clearing will help reduce
    253  *		the number of calls we make to the pmap layer.
    254  */
    255 
    256 struct vm_page **
    257 uvm_mk_pcluster(uobj, pps, npages, center, flags, mlo, mhi)
    258 	struct uvm_object *uobj;	/* IN */
    259 	struct vm_page **pps, *center;  /* IN/OUT, IN */
    260 	int *npages, flags;		/* IN/OUT, IN */
    261 	vaddr_t mlo, mhi;		/* IN (if !PGO_ALLPAGES) */
    262 {
    263 	struct vm_page **ppsp, *pclust;
    264 	vaddr_t lo, hi, curoff;
    265 	int center_idx, forward;
    266 	UVMHIST_FUNC("uvm_mk_pcluster"); UVMHIST_CALLED(maphist);
    267 
    268 	/*
    269 	 * center page should already be busy and write protected.  XXX:
    270 	 * suppose page is wired?  if we lock, then a process could
    271 	 * fault/block on it.  if we don't lock, a process could write the
    272 	 * pages in the middle of an I/O.  (consider an msync()).  let's
    273 	 * lock it for now (better to delay than corrupt data?).
    274 	 */
    275 
    276 	/*
    277 	 * get cluster boundaries, check sanity, and apply our limits as well.
    278 	 */
    279 
    280 	uobj->pgops->pgo_cluster(uobj, center->offset, &lo, &hi);
    281 	if ((flags & PGO_ALLPAGES) == 0) {
    282 		if (lo < mlo)
    283 			lo = mlo;
    284 		if (hi > mhi)
    285 			hi = mhi;
    286 	}
    287 	if ((hi - lo) >> PAGE_SHIFT > *npages) {  /* pps too small, bail out! */
    288 #ifdef DIAGNOSTIC
    289 	    printf("uvm_mk_pcluster: provided page array too small (fixed)\n");
    290 #endif
    291 		pps[0] = center;
    292 		*npages = 1;
    293 		return(pps);
    294 	}
    295 
    296 	/*
    297 	 * now determine the center and attempt to cluster around the
    298 	 * edges
    299 	 */
    300 
    301 	center_idx = (center->offset - lo) >> PAGE_SHIFT;
    302 	pps[center_idx] = center;	/* plug in the center page */
    303 	ppsp = &pps[center_idx];
    304 	*npages = 1;
    305 
    306 	/*
    307 	 * attempt to cluster around the left [backward], and then
    308 	 * the right side [forward].
    309 	 *
    310 	 * note that for inactive pages (pages that have been deactivated)
    311 	 * there are no valid mappings and PG_CLEAN should be up to date.
    312 	 * [i.e. there is no need to query the pmap with pmap_is_modified
    313 	 * since there are no mappings].
    314 	 */
    315 
    316 	for (forward  = 0 ; forward <= 1 ; forward++) {
    317 
    318 		curoff = center->offset + (forward ? PAGE_SIZE : -PAGE_SIZE);
    319 		for ( ;(forward == 0 && curoff >= lo) ||
    320 		       (forward && curoff < hi);
    321 		      curoff += (forward ? 1 : -1) << PAGE_SHIFT) {
    322 
    323 			pclust = uvm_pagelookup(uobj, curoff); /* lookup page */
    324 			if (pclust == NULL)
    325 				break;			/* no page */
    326 			/* handle active pages */
    327 			/* NOTE: inactive pages don't have pmap mappings */
    328 			if ((pclust->pqflags & PQ_INACTIVE) == 0) {
    329 				if ((flags & PGO_DOACTCLUST) == 0)
    330 					/* dont want mapped pages at all */
    331 					break;
    332 
    333 				/* make sure "clean" bit is sync'd */
    334 				if ((pclust->flags & PG_CLEANCHK) == 0) {
    335 					if ((pclust->flags & (PG_CLEAN|PG_BUSY))
    336 					   == PG_CLEAN &&
    337 					   pmap_is_modified(PMAP_PGARG(pclust)))
    338 					pclust->flags &= ~PG_CLEAN;
    339 					/* now checked */
    340 					pclust->flags |= PG_CLEANCHK;
    341 				}
    342 			}
    343 			/* is page available for cleaning and does it need it */
    344 			if ((pclust->flags & (PG_CLEAN|PG_BUSY)) != 0)
    345 				break;	/* page is already clean or is busy */
    346 
    347 			/* yes!   enroll the page in our array */
    348 			pclust->flags |= PG_BUSY;		/* busy! */
    349 			UVM_PAGE_OWN(pclust, "uvm_mk_pcluster");
    350 			/* XXX: protect wired page?   see above comment. */
    351 			pmap_page_protect(PMAP_PGARG(pclust), VM_PROT_READ);
    352 			if (!forward) {
    353 				ppsp--;			/* back up one page */
    354 				*ppsp = pclust;
    355 			} else {
    356 				/* move forward one page */
    357 				ppsp[*npages] = pclust;
    358 			}
    359 			*npages = *npages + 1;
    360 		}
    361 	}
    362 
    363 	/*
    364 	 * done!  return the cluster array to the caller!!!
    365 	 */
    366 
    367 	UVMHIST_LOG(maphist, "<- done",0,0,0,0);
    368 	return(ppsp);
    369 }
    370 
    371 
    372 /*
    373  * uvm_shareprot: generic share protect routine
    374  *
    375  * => caller must lock map entry's map
    376  * => caller must lock object pointed to by map entry
    377  */
    378 
    379 void
    380 uvm_shareprot(entry, prot)
    381 	vm_map_entry_t entry;
    382 	vm_prot_t prot;
    383 {
    384 	struct uvm_object *uobj = entry->object.uvm_obj;
    385 	struct vm_page *pp;
    386 	vaddr_t start, stop;
    387 	UVMHIST_FUNC("uvm_shareprot"); UVMHIST_CALLED(maphist);
    388 
    389 	if (UVM_ET_ISSUBMAP(entry))
    390 		panic("uvm_shareprot: non-object attached");
    391 
    392 	start = entry->offset;
    393 	stop = start + (entry->end - entry->start);
    394 
    395 	/*
    396 	 * traverse list of pages in object.   if page in range, pmap_prot it
    397 	 */
    398 
    399 	for (pp = uobj->memq.tqh_first ; pp != NULL ; pp = pp->listq.tqe_next) {
    400 		if (pp->offset >= start && pp->offset < stop)
    401 			pmap_page_protect(PMAP_PGARG(pp), prot);
    402 	}
    403 	UVMHIST_LOG(maphist, "<- done",0,0,0,0);
    404 }
    405 
    406 /*
    407  * uvm_pager_put: high level pageout routine
    408  *
    409  * we want to pageout page "pg" to backing store, clustering if
    410  * possible.
    411  *
    412  * => page queues must be locked by caller
    413  * => if page is not swap-backed, then "uobj" points to the object
    414  *	backing it.   this object should be locked by the caller.
    415  * => if page is swap-backed, then "uobj" should be NULL.
    416  * => "pg" should be PG_BUSY (by caller), and !PG_CLEAN
    417  *    for swap-backed memory, "pg" can be NULL if there is no page
    418  *    of interest [sometimes the case for the pagedaemon]
    419  * => "ppsp_ptr" should point to an array of npages vm_page pointers
    420  *	for possible cluster building
    421  * => flags (first two for non-swap-backed pages)
    422  *	PGO_ALLPAGES: all pages in uobj are valid targets
    423  *	PGO_DOACTCLUST: include "PQ_ACTIVE" pages as valid targets
    424  *	PGO_SYNCIO: do SYNC I/O (no async)
    425  *	PGO_PDFREECLUST: pagedaemon: drop cluster on successful I/O
    426  * => start/stop: if (uobj && !PGO_ALLPAGES) limit targets to this range
    427  *		  if (!uobj) start is the (daddr_t) of the starting swapblk
    428  * => return state:
    429  *	1. we return the VM_PAGER status code of the pageout
    430  *	2. we return with the page queues unlocked
    431  *	3. if (uobj != NULL) [!swap_backed] we return with
    432  *		uobj locked _only_ if PGO_PDFREECLUST is set
    433  *		AND result != VM_PAGER_PEND.   in all other cases
    434  *		we return with uobj unlocked.   [this is a hack
    435  *		that allows the pagedaemon to save one lock/unlock
    436  *		pair in the !swap_backed case since we have to
    437  *		lock the uobj to drop the cluster anyway]
    438  *	4. on errors we always drop the cluster.   thus, if we return
    439  *		!PEND, !OK, then the caller only has to worry about
    440  *		un-busying the main page (not the cluster pages).
    441  *	5. on success, if !PGO_PDFREECLUST, we return the cluster
    442  *		with all pages busy (caller must un-busy and check
    443  *		wanted/released flags).
    444  */
    445 
    446 int
    447 uvm_pager_put(uobj, pg, ppsp_ptr, npages, flags, start, stop)
    448 	struct uvm_object *uobj;	/* IN */
    449 	struct vm_page *pg, ***ppsp_ptr;/* IN, IN/OUT */
    450 	int *npages;			/* IN/OUT */
    451 	int flags;			/* IN */
    452 	vaddr_t start, stop;	/* IN, IN */
    453 {
    454 	int result;
    455 	daddr_t swblk;
    456 	struct vm_page **ppsp = *ppsp_ptr;
    457 
    458 	/*
    459 	 * note that uobj is null  if we are doing a swap-backed pageout.
    460 	 * note that uobj is !null if we are doing normal object pageout.
    461 	 * note that the page queues must be locked to cluster.
    462 	 */
    463 
    464 	if (uobj) {	/* if !swap-backed */
    465 
    466 		/*
    467 		 * attempt to build a cluster for pageout using its
    468 		 * make-put-cluster function (if it has one).
    469 		 */
    470 
    471 		if (uobj->pgops->pgo_mk_pcluster) {
    472 			ppsp = uobj->pgops->pgo_mk_pcluster(uobj, ppsp,
    473 			    npages, pg, flags, start, stop);
    474 			*ppsp_ptr = ppsp;  /* update caller's pointer */
    475 		} else {
    476 			ppsp[0] = pg;
    477 			*npages = 1;
    478 		 }
    479 
    480 		swblk = 0;		/* XXX: keep gcc happy */
    481 
    482 	} else {
    483 
    484 		/*
    485 		 * for swap-backed pageout, the caller (the pagedaemon) has
    486 		 * already built the cluster for us.   the starting swap
    487 		 * block we are writing to has been passed in as "start."
    488 		 * "pg" could be NULL if there is no page we are especially
    489 		 * interested in (in which case the whole cluster gets dropped
    490 		 * in the event of an error or a sync "done").
    491 		 */
    492 		swblk = (daddr_t) start;
    493 		/* ppsp and npages should be ok */
    494 	}
    495 
    496 	/* now that we've clustered we can unlock the page queues */
    497 	uvm_unlock_pageq();
    498 
    499 	/*
    500 	 * now attempt the I/O.   if we have a failure and we are
    501 	 * clustered, we will drop the cluster and try again.
    502 	 */
    503 
    504 ReTry:
    505 	if (uobj) {
    506 		/* object is locked */
    507 		result = uobj->pgops->pgo_put(uobj, ppsp, *npages,
    508 		    flags & PGO_SYNCIO);
    509 		/* object is now unlocked */
    510 	} else {
    511 		/* nothing locked */
    512 		result = uvm_swap_put(swblk, ppsp, *npages, flags & PGO_SYNCIO);
    513 		/* nothing locked */
    514 	}
    515 
    516 	/*
    517 	 * we have attempted the I/O.
    518 	 *
    519 	 * if the I/O was a success then:
    520 	 * 	if !PGO_PDFREECLUST, we return the cluster to the
    521 	 *		caller (who must un-busy all pages)
    522 	 *	else we un-busy cluster pages for the pagedaemon
    523 	 *
    524 	 * if I/O is pending (async i/o) then we return the pending code.
    525 	 * [in this case the async i/o done function must clean up when
    526 	 *  i/o is done...]
    527 	 */
    528 
    529 	if (result == VM_PAGER_PEND || result == VM_PAGER_OK) {
    530 		if (result == VM_PAGER_OK && (flags & PGO_PDFREECLUST)) {
    531 			/*
    532 			 * drop cluster and relock object (only if I/O is
    533 			 * not pending)
    534 			 */
    535 			if (uobj)
    536 				/* required for dropcluster */
    537 				simple_lock(&uobj->vmobjlock);
    538 			if (*npages > 1 || pg == NULL)
    539 				uvm_pager_dropcluster(uobj, pg, ppsp, npages,
    540 				    PGO_PDFREECLUST, 0);
    541 			/* if (uobj): object still locked, as per
    542 			 * return-state item #3 */
    543 		}
    544 		return (result);
    545 	}
    546 
    547 	/*
    548 	 * a pager error occured.    if we have clustered, we drop the
    549 	 * cluster and try again.
    550 	 */
    551 
    552 	if (*npages > 1 || pg == NULL) {
    553 		if (uobj)
    554 			simple_lock(&uobj->vmobjlock);
    555 		uvm_pager_dropcluster(uobj, pg, ppsp, npages, PGO_REALLOCSWAP,
    556 		    swblk);
    557 		if (pg != NULL)
    558 			goto ReTry;
    559 	}
    560 
    561 	/*
    562 	 * a pager error occured (even after dropping the cluster, if there
    563 	 * was one).    give up!   the caller only has one page ("pg")
    564 	 * to worry about.
    565 	 */
    566 
    567 	if (uobj && (flags & PGO_PDFREECLUST) != 0)
    568 		simple_lock(&uobj->vmobjlock);
    569 	return(result);
    570 }
    571 
    572 /*
    573  * uvm_pager_dropcluster: drop a cluster we have built (because we
    574  * got an error, or, if PGO_PDFREECLUST we are un-busying the
    575  * cluster pages on behalf of the pagedaemon).
    576  *
    577  * => uobj, if non-null, is a non-swap-backed object that is
    578  *	locked by the caller.   we return with this object still
    579  *	locked.
    580  * => page queues are not locked
    581  * => pg is our page of interest (the one we clustered around, can be null)
    582  * => ppsp/npages is our current cluster
    583  * => flags: PGO_PDFREECLUST: pageout was a success: un-busy cluster
    584  *	pages on behalf of the pagedaemon.
    585  *           PGO_REALLOCSWAP: drop previously allocated swap slots for
    586  *		clustered swap-backed pages (except for "pg" if !NULL)
    587  *		"swblk" is the start of swap alloc (e.g. for ppsp[0])
    588  *		[only meaningful if swap-backed (uobj == NULL)]
    589  */
    590 
    591 
    592 void uvm_pager_dropcluster(uobj, pg, ppsp, npages, flags, swblk)
    593 
    594 struct uvm_object *uobj;	/* IN */
    595 struct vm_page *pg, **ppsp;	/* IN, IN/OUT */
    596 int *npages;			/* IN/OUT */
    597 int flags;
    598 int swblk;			/* valid if (uobj == NULL && PGO_REALLOCSWAP) */
    599 
    600 {
    601 	int lcv;
    602 	boolean_t obj_is_alive;
    603 	struct uvm_object *saved_uobj;
    604 
    605 	/*
    606 	 * if we need to reallocate swap space for the cluster we are dropping
    607 	 * (true if swap-backed and PGO_REALLOCSWAP) then free the old
    608 	 * allocation now.   save a block for "pg" if it is non-NULL.
    609 	 *
    610 	 * note that we will zap the object's pointer to swap in the "for" loop
    611 	 * below...
    612 	 */
    613 
    614 	if (uobj == NULL && (flags & PGO_REALLOCSWAP)) {
    615 		if (pg)
    616 			uvm_swap_free(swblk + 1, *npages - 1);
    617 		else
    618 			uvm_swap_free(swblk, *npages);
    619 	}
    620 
    621 	/*
    622 	 * drop all pages but "pg"
    623 	 */
    624 
    625 	for (lcv = 0 ; lcv < *npages ; lcv++) {
    626 
    627 		if (ppsp[lcv] == pg)		/* skip "pg" */
    628 			continue;
    629 
    630 		/*
    631 		 * if swap-backed, gain lock on object that owns page.  note
    632 		 * that PQ_ANON bit can't change as long as we are holding
    633 		 * the PG_BUSY bit (so there is no need to lock the page
    634 		 * queues to test it).
    635 		 *
    636 		 * once we have the lock, dispose of the pointer to swap, if
    637 		 * requested
    638 		 */
    639 		if (!uobj) {
    640 			if (ppsp[lcv]->pqflags & PQ_ANON) {
    641 				simple_lock(&ppsp[lcv]->uanon->an_lock);
    642 				if (flags & PGO_REALLOCSWAP)
    643 					  /* zap swap block */
    644 					  ppsp[lcv]->uanon->an_swslot = 0;
    645 			} else {
    646 				simple_lock(&ppsp[lcv]->uobject->vmobjlock);
    647 				if (flags & PGO_REALLOCSWAP)
    648 					uao_set_swslot(ppsp[lcv]->uobject,
    649 					    ppsp[lcv]->offset >> PAGE_SHIFT, 0);
    650 			}
    651 		}
    652 
    653 		/* did someone want the page while we had it busy-locked? */
    654 		if (ppsp[lcv]->flags & PG_WANTED)
    655 			/* still holding obj lock */
    656 			thread_wakeup(ppsp[lcv]);
    657 
    658 		/* if page was released, release it.  otherwise un-busy it */
    659 		if (ppsp[lcv]->flags & PG_RELEASED) {
    660 
    661 			if (ppsp[lcv]->pqflags & PQ_ANON) {
    662 				/* so that anfree will free */
    663 				ppsp[lcv]->flags &= ~(PG_BUSY);
    664 				UVM_PAGE_OWN(ppsp[lcv], NULL);
    665 
    666 				pmap_page_protect(PMAP_PGARG(ppsp[lcv]),
    667 				    VM_PROT_NONE); /* be safe */
    668 				simple_unlock(&ppsp[lcv]->uanon->an_lock);
    669 				/* kills anon and frees pg */
    670 				uvm_anfree(ppsp[lcv]->uanon);
    671 
    672 				continue;
    673 			}
    674 
    675 			/*
    676 			 * pgo_releasepg will dump the page for us
    677 			 */
    678 
    679 #ifdef DIAGNOSTIC
    680 			if (ppsp[lcv]->uobject->pgops->pgo_releasepg == NULL)
    681 				panic("uvm_pager_dropcluster: no releasepg "
    682 				    "function");
    683 #endif
    684 			saved_uobj = ppsp[lcv]->uobject;
    685 			obj_is_alive =
    686 			    saved_uobj->pgops->pgo_releasepg(ppsp[lcv], NULL);
    687 
    688 #ifdef DIAGNOSTIC
    689 			/* for normal objects, "pg" is still PG_BUSY by us,
    690 			 * so obj can't die */
    691 			if (uobj && !obj_is_alive)
    692 				panic("uvm_pager_dropcluster: object died "
    693 				    "with active page");
    694 #endif
    695 			/* only unlock the object if it is still alive...  */
    696 			if (obj_is_alive && saved_uobj != uobj)
    697 				simple_unlock(&saved_uobj->vmobjlock);
    698 
    699 			/*
    700 			 * XXXCDC: suppose uobj died in the pgo_releasepg?
    701 			 * how pass that
    702 			 * info up to caller.  we are currently ignoring it...
    703 			 */
    704 
    705 			continue;		/* next page */
    706 
    707 		} else {
    708 			ppsp[lcv]->flags &= ~(PG_BUSY|PG_WANTED);
    709 			UVM_PAGE_OWN(ppsp[lcv], NULL);
    710 		}
    711 
    712 		/*
    713 		 * if we are operating on behalf of the pagedaemon and we
    714 		 * had a successful pageout update the page!
    715 		 */
    716 		if (flags & PGO_PDFREECLUST) {
    717 			/* XXX: with PMAP_NEW ref should already be clear,
    718 			 * but don't trust! */
    719 			pmap_clear_reference(PMAP_PGARG(ppsp[lcv]));
    720 			pmap_clear_modify(PMAP_PGARG(ppsp[lcv]));
    721 			ppsp[lcv]->flags |= PG_CLEAN;
    722 		}
    723 
    724 		/* if anonymous cluster, unlock object and move on */
    725 		if (!uobj) {
    726 			if (ppsp[lcv]->pqflags & PQ_ANON)
    727 				simple_unlock(&ppsp[lcv]->uanon->an_lock);
    728 			else
    729 				simple_unlock(&ppsp[lcv]->uobject->vmobjlock);
    730 		}
    731 
    732 	}
    733 
    734 	/*
    735 	 * drop to a cluster of 1 page ("pg") if requested
    736 	 */
    737 
    738 	if (pg && (flags & PGO_PDFREECLUST) == 0) {
    739 		/*
    740 		 * if we are not a successful pageout, we make a 1 page cluster.
    741 		 */
    742 		ppsp[0] = pg;
    743 		*npages = 1;
    744 
    745 		/*
    746 		 * assign new swap block to new cluster, if anon backed
    747 		 */
    748 		if (uobj == NULL && (flags & PGO_REALLOCSWAP)) {
    749 			if (pg->pqflags & PQ_ANON) {
    750 				simple_lock(&pg->uanon->an_lock);
    751 				pg->uanon->an_swslot = swblk;	/* reassign */
    752 				simple_unlock(&pg->uanon->an_lock);
    753 			} else {
    754 				simple_lock(&pg->uobject->vmobjlock);
    755 				uao_set_swslot(pg->uobject,
    756 				    pg->offset >> PAGE_SHIFT, swblk);
    757 				simple_unlock(&pg->uobject->vmobjlock);
    758 			}
    759 		}
    760 	}
    761 }
    762