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