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