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