Home | History | Annotate | Line # | Download | only in uvm
uvm_pager.c revision 1.68.2.8
      1  1.68.2.8     yamt /*	$NetBSD: uvm_pager.c,v 1.68.2.8 2008/03/17 09:15:52 yamt 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.1      mrg /*
     38       1.1      mrg  * uvm_pager.c: generic functions used to assist the pagers.
     39       1.1      mrg  */
     40      1.54    lukem 
     41      1.54    lukem #include <sys/cdefs.h>
     42  1.68.2.8     yamt __KERNEL_RCSID(0, "$NetBSD: uvm_pager.c,v 1.68.2.8 2008/03/17 09:15:52 yamt Exp $");
     43      1.54    lukem 
     44      1.54    lukem #include "opt_uvmhist.h"
     45  1.68.2.1     yamt #include "opt_readahead.h"
     46  1.68.2.5     yamt #include "opt_pagermap.h"
     47       1.1      mrg 
     48       1.1      mrg #include <sys/param.h>
     49       1.1      mrg #include <sys/systm.h>
     50       1.1      mrg #include <sys/proc.h>
     51       1.1      mrg #include <sys/malloc.h>
     52      1.35      chs #include <sys/pool.h>
     53      1.35      chs #include <sys/vnode.h>
     54       1.1      mrg 
     55       1.1      mrg #include <uvm/uvm.h>
     56       1.1      mrg 
     57  1.68.2.5     yamt /*
     58  1.68.2.5     yamt  * XXX
     59  1.68.2.5     yamt  * this is needed until the device strategy interface
     60  1.68.2.5     yamt  * is changed to do physically-addressed i/o.
     61  1.68.2.5     yamt  */
     62  1.68.2.5     yamt 
     63  1.68.2.5     yamt #ifndef PAGER_MAP_DEFAULT_SIZE
     64  1.68.2.5     yamt #define PAGER_MAP_DEFAULT_SIZE	(16 * 1024 * 1024)
     65  1.68.2.5     yamt #endif
     66  1.68.2.5     yamt 
     67  1.68.2.5     yamt #ifndef PAGER_MAP_SIZE
     68  1.68.2.5     yamt #define PAGER_MAP_SIZE	PAGER_MAP_DEFAULT_SIZE
     69  1.68.2.5     yamt #endif
     70  1.68.2.5     yamt 
     71  1.68.2.5     yamt size_t pager_map_size = PAGER_MAP_SIZE;
     72  1.68.2.5     yamt 
     73      1.35      chs struct pool *uvm_aiobuf_pool;
     74      1.35      chs 
     75       1.1      mrg /*
     76       1.1      mrg  * list of uvm pagers in the system
     77       1.1      mrg  */
     78       1.1      mrg 
     79  1.68.2.6     yamt const struct uvm_pagerops * const uvmpagerops[] = {
     80      1.10  thorpej 	&aobj_pager,
     81       1.6      mrg 	&uvm_deviceops,
     82       1.6      mrg 	&uvm_vnodeops,
     83      1.35      chs 	&ubc_pager,
     84       1.1      mrg };
     85       1.1      mrg 
     86       1.1      mrg /*
     87       1.1      mrg  * the pager map: provides KVA for I/O
     88       1.1      mrg  */
     89       1.1      mrg 
     90      1.47      chs struct vm_map *pager_map;		/* XXX */
     91  1.68.2.4     yamt kmutex_t pager_map_wanted_lock;
     92  1.68.2.3     yamt bool pager_map_wanted;	/* locked by pager map */
     93      1.35      chs static vaddr_t emergva;
     94  1.68.2.3     yamt static bool emerginuse;
     95       1.1      mrg 
     96       1.1      mrg /*
     97       1.1      mrg  * uvm_pager_init: init pagers (at boot time)
     98       1.1      mrg  */
     99       1.1      mrg 
    100       1.6      mrg void
    101      1.67  thorpej uvm_pager_init(void)
    102       1.6      mrg {
    103      1.59  thorpej 	u_int lcv;
    104      1.50      chs 	vaddr_t sva, eva;
    105       1.1      mrg 
    106       1.6      mrg 	/*
    107       1.6      mrg 	 * init pager map
    108       1.6      mrg 	 */
    109       1.6      mrg 
    110      1.50      chs 	sva = 0;
    111  1.68.2.5     yamt 	pager_map = uvm_km_suballoc(kernel_map, &sva, &eva, pager_map_size, 0,
    112  1.68.2.3     yamt 	    false, NULL);
    113  1.68.2.4     yamt 	mutex_init(&pager_map_wanted_lock, MUTEX_DEFAULT, IPL_NONE);
    114  1.68.2.3     yamt 	pager_map_wanted = false;
    115      1.66     yamt 	emergva = uvm_km_alloc(kernel_map, round_page(MAXPHYS), 0,
    116      1.66     yamt 	    UVM_KMF_VAONLY);
    117      1.66     yamt #if defined(DEBUG)
    118      1.66     yamt 	if (emergva == 0)
    119      1.66     yamt 		panic("emergva");
    120      1.66     yamt #endif
    121  1.68.2.3     yamt 	emerginuse = false;
    122       1.6      mrg 
    123       1.6      mrg 	/*
    124       1.6      mrg 	 * init ASYNC I/O queue
    125       1.6      mrg 	 */
    126      1.45      chs 
    127       1.6      mrg 	TAILQ_INIT(&uvm.aio_done);
    128       1.1      mrg 
    129       1.6      mrg 	/*
    130       1.6      mrg 	 * call pager init functions
    131       1.6      mrg 	 */
    132  1.68.2.6     yamt 	for (lcv = 0 ; lcv < __arraycount(uvmpagerops); lcv++) {
    133       1.6      mrg 		if (uvmpagerops[lcv]->pgo_init)
    134       1.6      mrg 			uvmpagerops[lcv]->pgo_init();
    135       1.6      mrg 	}
    136       1.1      mrg }
    137       1.1      mrg 
    138       1.1      mrg /*
    139       1.1      mrg  * uvm_pagermapin: map pages into KVA (pager_map) for I/O that needs mappings
    140       1.1      mrg  *
    141       1.1      mrg  * we basically just map in a blank map entry to reserve the space in the
    142       1.1      mrg  * map and then use pmap_enter() to put the mappings in by hand.
    143       1.1      mrg  */
    144       1.1      mrg 
    145       1.9      eeh vaddr_t
    146      1.67  thorpej uvm_pagermapin(struct vm_page **pps, int npages, int flags)
    147       1.1      mrg {
    148       1.9      eeh 	vsize_t size;
    149       1.9      eeh 	vaddr_t kva;
    150       1.9      eeh 	vaddr_t cva;
    151       1.6      mrg 	struct vm_page *pp;
    152      1.29  thorpej 	vm_prot_t prot;
    153  1.68.2.4     yamt 	const bool pdaemon = curlwp == uvm.pagedaemon_lwp;
    154       1.6      mrg 	UVMHIST_FUNC("uvm_pagermapin"); UVMHIST_CALLED(maphist);
    155       1.1      mrg 
    156      1.35      chs 	UVMHIST_LOG(maphist,"(pps=0x%x, npages=%d)", pps, npages,0,0);
    157      1.29  thorpej 
    158      1.29  thorpej 	/*
    159      1.29  thorpej 	 * compute protection.  outgoing I/O only needs read
    160      1.29  thorpej 	 * access to the page, whereas incoming needs read/write.
    161      1.29  thorpej 	 */
    162      1.29  thorpej 
    163      1.29  thorpej 	prot = VM_PROT_READ;
    164      1.29  thorpej 	if (flags & UVMPAGER_MAPIN_READ)
    165      1.29  thorpej 		prot |= VM_PROT_WRITE;
    166       1.1      mrg 
    167       1.1      mrg ReStart:
    168      1.12      chs 	size = npages << PAGE_SHIFT;
    169      1.29  thorpej 	kva = 0;			/* let system choose VA */
    170       1.1      mrg 
    171  1.68.2.1     yamt 	if (uvm_map(pager_map, &kva, size, NULL, UVM_UNKNOWN_OFFSET, 0,
    172  1.68.2.1     yamt 	    UVM_FLAG_NOMERGE | (pdaemon ? UVM_FLAG_NOWAIT : 0)) != 0) {
    173  1.68.2.1     yamt 		if (pdaemon) {
    174  1.68.2.4     yamt 			mutex_enter(&pager_map_wanted_lock);
    175      1.35      chs 			if (emerginuse) {
    176  1.68.2.7     yamt 				UVM_UNLOCK_AND_WAIT(&emergva,
    177  1.68.2.7     yamt 				    &pager_map_wanted_lock, false,
    178  1.68.2.7     yamt 				    "emergva", 0);
    179      1.35      chs 				goto ReStart;
    180      1.35      chs 			}
    181  1.68.2.3     yamt 			emerginuse = true;
    182  1.68.2.4     yamt 			mutex_exit(&pager_map_wanted_lock);
    183      1.35      chs 			kva = emergva;
    184      1.60      tls 			/* The shift implicitly truncates to PAGE_SIZE */
    185      1.60      tls 			KASSERT(npages <= (MAXPHYS >> PAGE_SHIFT));
    186      1.35      chs 			goto enter;
    187      1.35      chs 		}
    188      1.29  thorpej 		if ((flags & UVMPAGER_MAPIN_WAITOK) == 0) {
    189       1.6      mrg 			UVMHIST_LOG(maphist,"<- NOWAIT failed", 0,0,0,0);
    190      1.29  thorpej 			return(0);
    191       1.6      mrg 		}
    192  1.68.2.4     yamt 		mutex_enter(&pager_map_wanted_lock);
    193  1.68.2.3     yamt 		pager_map_wanted = true;
    194       1.6      mrg 		UVMHIST_LOG(maphist, "  SLEEPING on pager_map",0,0,0,0);
    195  1.68.2.7     yamt 		UVM_UNLOCK_AND_WAIT(pager_map, &pager_map_wanted_lock, false,
    196  1.68.2.7     yamt 		    "pager_map", 0);
    197       1.6      mrg 		goto ReStart;
    198       1.6      mrg 	}
    199       1.1      mrg 
    200      1.35      chs enter:
    201       1.6      mrg 	/* got it */
    202       1.6      mrg 	for (cva = kva ; size != 0 ; size -= PAGE_SIZE, cva += PAGE_SIZE) {
    203       1.6      mrg 		pp = *pps++;
    204      1.40      mrg 		KASSERT(pp);
    205      1.38      chs 		KASSERT(pp->flags & PG_BUSY);
    206      1.50      chs 		pmap_kenter_pa(cva, VM_PAGE_TO_PHYS(pp), prot);
    207       1.6      mrg 	}
    208      1.49    chris 	pmap_update(vm_map_pmap(pager_map));
    209       1.1      mrg 
    210       1.6      mrg 	UVMHIST_LOG(maphist, "<- done (KVA=0x%x)", kva,0,0,0);
    211       1.6      mrg 	return(kva);
    212       1.1      mrg }
    213       1.1      mrg 
    214       1.1      mrg /*
    215       1.1      mrg  * uvm_pagermapout: remove pager_map mapping
    216       1.1      mrg  *
    217       1.1      mrg  * we remove our mappings by hand and then remove the mapping (waking
    218       1.1      mrg  * up anyone wanting space).
    219       1.1      mrg  */
    220       1.1      mrg 
    221       1.6      mrg void
    222      1.67  thorpej uvm_pagermapout(vaddr_t kva, int npages)
    223       1.6      mrg {
    224      1.12      chs 	vsize_t size = npages << PAGE_SHIFT;
    225      1.47      chs 	struct vm_map_entry *entries;
    226       1.6      mrg 	UVMHIST_FUNC("uvm_pagermapout"); UVMHIST_CALLED(maphist);
    227      1.35      chs 
    228       1.6      mrg 	UVMHIST_LOG(maphist, " (kva=0x%x, npages=%d)", kva, npages,0,0);
    229       1.1      mrg 
    230       1.6      mrg 	/*
    231       1.6      mrg 	 * duplicate uvm_unmap, but add in pager_map_wanted handling.
    232       1.6      mrg 	 */
    233       1.6      mrg 
    234      1.50      chs 	pmap_kremove(kva, npages << PAGE_SHIFT);
    235      1.35      chs 	if (kva == emergva) {
    236  1.68.2.4     yamt 		mutex_enter(&pager_map_wanted_lock);
    237  1.68.2.3     yamt 		emerginuse = false;
    238      1.35      chs 		wakeup(&emergva);
    239  1.68.2.4     yamt 		mutex_exit(&pager_map_wanted_lock);
    240      1.50      chs 		return;
    241      1.35      chs 	}
    242      1.35      chs 
    243       1.6      mrg 	vm_map_lock(pager_map);
    244      1.66     yamt 	uvm_unmap_remove(pager_map, kva, kva + size, &entries, NULL, 0);
    245  1.68.2.4     yamt 	mutex_enter(&pager_map_wanted_lock);
    246       1.6      mrg 	if (pager_map_wanted) {
    247  1.68.2.3     yamt 		pager_map_wanted = false;
    248       1.6      mrg 		wakeup(pager_map);
    249       1.6      mrg 	}
    250  1.68.2.4     yamt 	mutex_exit(&pager_map_wanted_lock);
    251       1.6      mrg 	vm_map_unlock(pager_map);
    252       1.6      mrg 	if (entries)
    253       1.6      mrg 		uvm_unmap_detach(entries, 0);
    254      1.49    chris 	pmap_update(pmap_kernel());
    255       1.6      mrg 	UVMHIST_LOG(maphist,"<- done",0,0,0,0);
    256       1.1      mrg }
    257       1.1      mrg 
    258       1.1      mrg /*
    259      1.35      chs  * interrupt-context iodone handler for nested i/o bufs.
    260      1.35      chs  *
    261  1.68.2.7     yamt  * => the buffer is private so need not be locked here
    262      1.35      chs  */
    263      1.35      chs 
    264      1.35      chs void
    265      1.67  thorpej uvm_aio_biodone1(struct buf *bp)
    266      1.35      chs {
    267      1.35      chs 	struct buf *mbp = bp->b_private;
    268      1.35      chs 
    269      1.35      chs 	KASSERT(mbp != bp);
    270  1.68.2.7     yamt 	if (bp->b_error != 0) {
    271      1.35      chs 		mbp->b_error = bp->b_error;
    272  1.68.2.7     yamt 	}
    273      1.35      chs 	mbp->b_resid -= bp->b_bcount;
    274  1.68.2.1     yamt 	putiobuf(bp);
    275      1.35      chs 	if (mbp->b_resid == 0) {
    276      1.35      chs 		biodone(mbp);
    277      1.35      chs 	}
    278      1.35      chs }
    279      1.35      chs 
    280      1.35      chs /*
    281      1.35      chs  * interrupt-context iodone handler for single-buf i/os
    282      1.35      chs  * or the top-level buf of a nested-buf i/o.
    283      1.35      chs  */
    284      1.35      chs 
    285      1.35      chs void
    286      1.67  thorpej uvm_aio_biodone(struct buf *bp)
    287      1.35      chs {
    288      1.35      chs 	/* reset b_iodone for when this is a single-buf i/o. */
    289      1.35      chs 	bp->b_iodone = uvm_aio_aiodone;
    290      1.35      chs 
    291  1.68.2.4     yamt 	workqueue_enqueue(uvm.aiodone_queue, &bp->b_work, NULL);
    292      1.35      chs }
    293      1.35      chs 
    294      1.35      chs void
    295  1.68.2.8     yamt uvm_aio_aiodone_pages(struct vm_page **pgs, int npages, bool write, int error)
    296      1.35      chs {
    297      1.35      chs 	struct uvm_object *uobj;
    298  1.68.2.8     yamt 	struct vm_page *pg;
    299  1.68.2.7     yamt 	kmutex_t *slock;
    300  1.68.2.8     yamt 	int pageout_done;
    301  1.68.2.8     yamt 	int swslot;
    302  1.68.2.8     yamt 	int i;
    303  1.68.2.8     yamt 	bool swap;
    304      1.50      chs 
    305      1.50      chs 	swslot = 0;
    306  1.68.2.8     yamt 	pageout_done = 0;
    307      1.50      chs 	slock = NULL;
    308  1.68.2.8     yamt 	uobj = NULL;
    309      1.55      chs 	pg = pgs[0];
    310      1.55      chs 	swap = (pg->uanon != NULL && pg->uobject == NULL) ||
    311      1.55      chs 		(pg->pqflags & PQ_AOBJ) != 0;
    312      1.50      chs 	if (!swap) {
    313      1.55      chs 		uobj = pg->uobject;
    314      1.50      chs 		slock = &uobj->vmobjlock;
    315  1.68.2.7     yamt 		mutex_enter(slock);
    316  1.68.2.7     yamt 		mutex_enter(&uvm_pageqlock);
    317  1.68.2.1     yamt 	} else {
    318  1.68.2.1     yamt #if defined(VMSWAP)
    319  1.68.2.1     yamt 		if (error) {
    320  1.68.2.1     yamt 			if (pg->uobject != NULL) {
    321  1.68.2.1     yamt 				swslot = uao_find_swslot(pg->uobject,
    322  1.68.2.1     yamt 				    pg->offset >> PAGE_SHIFT);
    323  1.68.2.1     yamt 			} else {
    324  1.68.2.1     yamt 				KASSERT(pg->uanon != NULL);
    325  1.68.2.1     yamt 				swslot = pg->uanon->an_swslot;
    326  1.68.2.1     yamt 			}
    327  1.68.2.1     yamt 			KASSERT(swslot);
    328      1.50      chs 		}
    329  1.68.2.1     yamt #else /* defined(VMSWAP) */
    330  1.68.2.1     yamt 		panic("%s: swap", __func__);
    331  1.68.2.1     yamt #endif /* defined(VMSWAP) */
    332      1.50      chs 	}
    333      1.35      chs 	for (i = 0; i < npages; i++) {
    334      1.35      chs 		pg = pgs[i];
    335      1.50      chs 		KASSERT(swap || pg->uobject == uobj);
    336      1.50      chs 		UVMHIST_LOG(ubchist, "pg %p", pg, 0,0,0);
    337      1.50      chs 
    338  1.68.2.1     yamt #if defined(VMSWAP)
    339      1.50      chs 		/*
    340      1.50      chs 		 * for swap i/os, lock each page's object (or anon)
    341      1.50      chs 		 * individually since each page may need a different lock.
    342      1.50      chs 		 */
    343      1.35      chs 
    344      1.35      chs 		if (swap) {
    345      1.55      chs 			if (pg->uobject != NULL) {
    346      1.55      chs 				slock = &pg->uobject->vmobjlock;
    347      1.55      chs 			} else {
    348      1.50      chs 				slock = &pg->uanon->an_lock;
    349      1.35      chs 			}
    350  1.68.2.7     yamt 			mutex_enter(slock);
    351  1.68.2.7     yamt 			mutex_enter(&uvm_pageqlock);
    352      1.50      chs 		}
    353  1.68.2.1     yamt #endif /* defined(VMSWAP) */
    354      1.50      chs 
    355      1.50      chs 		/*
    356      1.50      chs 		 * process errors.  for reads, just mark the page to be freed.
    357      1.50      chs 		 * for writes, if the error was ENOMEM, we assume this was
    358      1.50      chs 		 * a transient failure so we mark the page dirty so that
    359      1.50      chs 		 * we'll try to write it again later.  for all other write
    360      1.50      chs 		 * errors, we assume the error is permanent, thus the data
    361      1.50      chs 		 * in the page is lost.  bummer.
    362      1.50      chs 		 */
    363      1.50      chs 
    364      1.50      chs 		if (error) {
    365      1.61       pk 			int slot;
    366      1.50      chs 			if (!write) {
    367      1.50      chs 				pg->flags |= PG_RELEASED;
    368      1.50      chs 				continue;
    369      1.50      chs 			} else if (error == ENOMEM) {
    370      1.50      chs 				if (pg->flags & PG_PAGEOUT) {
    371      1.50      chs 					pg->flags &= ~PG_PAGEOUT;
    372  1.68.2.7     yamt 					pageout_done++;
    373      1.50      chs 				}
    374      1.50      chs 				pg->flags &= ~PG_CLEAN;
    375      1.50      chs 				uvm_pageactivate(pg);
    376      1.61       pk 				slot = 0;
    377      1.61       pk 			} else
    378      1.61       pk 				slot = SWSLOT_BAD;
    379      1.61       pk 
    380  1.68.2.1     yamt #if defined(VMSWAP)
    381      1.61       pk 			if (swap) {
    382      1.61       pk 				if (pg->uobject != NULL) {
    383      1.62       pk 					int oldslot;
    384      1.62       pk 					oldslot = uao_set_swslot(pg->uobject,
    385      1.62       pk 						pg->offset >> PAGE_SHIFT, slot);
    386      1.62       pk 					KASSERT(oldslot == swslot + i);
    387      1.61       pk 				} else {
    388      1.61       pk 					KASSERT(pg->uanon->an_swslot ==
    389      1.61       pk 						swslot + i);
    390      1.61       pk 					pg->uanon->an_swslot = slot;
    391      1.61       pk 				}
    392      1.50      chs 			}
    393  1.68.2.1     yamt #endif /* defined(VMSWAP) */
    394      1.50      chs 		}
    395      1.50      chs 
    396      1.50      chs 		/*
    397      1.50      chs 		 * if the page is PG_FAKE, this must have been a read to
    398      1.50      chs 		 * initialize the page.  clear PG_FAKE and activate the page.
    399      1.53      chs 		 * we must also clear the pmap "modified" flag since it may
    400      1.53      chs 		 * still be set from the page's previous identity.
    401      1.50      chs 		 */
    402      1.50      chs 
    403      1.50      chs 		if (pg->flags & PG_FAKE) {
    404      1.50      chs 			KASSERT(!write);
    405      1.50      chs 			pg->flags &= ~PG_FAKE;
    406  1.68.2.1     yamt #if defined(READAHEAD_STATS)
    407  1.68.2.2     yamt 			pg->pqflags |= PQ_READAHEAD;
    408  1.68.2.1     yamt 			uvm_ra_total.ev_count++;
    409  1.68.2.1     yamt #endif /* defined(READAHEAD_STATS) */
    410  1.68.2.2     yamt 			KASSERT((pg->flags & PG_CLEAN) != 0);
    411  1.68.2.2     yamt 			uvm_pageenqueue(pg);
    412      1.50      chs 			pmap_clear_modify(pg);
    413      1.35      chs 		}
    414      1.35      chs 
    415      1.35      chs 		/*
    416      1.53      chs 		 * do accounting for pagedaemon i/o and arrange to free
    417      1.53      chs 		 * the pages instead of just unbusying them.
    418      1.35      chs 		 */
    419      1.35      chs 
    420      1.53      chs 		if (pg->flags & PG_PAGEOUT) {
    421      1.50      chs 			pg->flags &= ~PG_PAGEOUT;
    422  1.68.2.7     yamt 			pageout_done++;
    423      1.64    enami 			uvmexp.pdfreed++;
    424      1.35      chs 			pg->flags |= PG_RELEASED;
    425      1.35      chs 		}
    426      1.35      chs 
    427  1.68.2.1     yamt #if defined(VMSWAP)
    428      1.35      chs 		/*
    429      1.50      chs 		 * for swap pages, unlock everything for this page now.
    430      1.35      chs 		 */
    431      1.35      chs 
    432      1.35      chs 		if (swap) {
    433      1.63     yamt 			if (pg->uobject == NULL && pg->uanon->an_ref == 0 &&
    434      1.63     yamt 			    (pg->flags & PG_RELEASED) != 0) {
    435  1.68.2.7     yamt 				mutex_exit(&uvm_pageqlock);
    436      1.63     yamt 				uvm_anon_release(pg->uanon);
    437      1.63     yamt 			} else {
    438      1.63     yamt 				uvm_page_unbusy(&pg, 1);
    439  1.68.2.7     yamt 				mutex_exit(&uvm_pageqlock);
    440  1.68.2.7     yamt 				mutex_exit(slock);
    441      1.63     yamt 			}
    442      1.35      chs 		}
    443  1.68.2.1     yamt #endif /* defined(VMSWAP) */
    444      1.35      chs 	}
    445  1.68.2.7     yamt 	uvm_pageout_done(pageout_done);
    446      1.35      chs 	if (!swap) {
    447      1.50      chs 		uvm_page_unbusy(pgs, npages);
    448  1.68.2.7     yamt 		mutex_exit(&uvm_pageqlock);
    449  1.68.2.7     yamt 		mutex_exit(slock);
    450      1.50      chs 	} else {
    451  1.68.2.1     yamt #if defined(VMSWAP)
    452      1.53      chs 		KASSERT(write);
    453      1.53      chs 
    454      1.53      chs 		/* these pages are now only in swap. */
    455  1.68.2.4     yamt 		mutex_enter(&uvm_swap_data_lock);
    456      1.53      chs 		KASSERT(uvmexp.swpgonly + npages <= uvmexp.swpginuse);
    457      1.61       pk 		if (error != ENOMEM)
    458      1.61       pk 			uvmexp.swpgonly += npages;
    459  1.68.2.4     yamt 		mutex_exit(&uvm_swap_data_lock);
    460      1.50      chs 		if (error) {
    461      1.61       pk 			if (error != ENOMEM)
    462      1.61       pk 				uvm_swap_markbad(swslot, npages);
    463      1.61       pk 			else
    464      1.61       pk 				uvm_swap_free(swslot, npages);
    465      1.50      chs 		}
    466      1.61       pk 		uvmexp.pdpending--;
    467  1.68.2.1     yamt #endif /* defined(VMSWAP) */
    468      1.35      chs 	}
    469  1.68.2.8     yamt }
    470  1.68.2.8     yamt 
    471  1.68.2.8     yamt /*
    472  1.68.2.8     yamt  * uvm_aio_aiodone: do iodone processing for async i/os.
    473  1.68.2.8     yamt  * this should be called in thread context, not interrupt context.
    474  1.68.2.8     yamt  */
    475  1.68.2.8     yamt 
    476  1.68.2.8     yamt void
    477  1.68.2.8     yamt uvm_aio_aiodone(struct buf *bp)
    478  1.68.2.8     yamt {
    479  1.68.2.8     yamt 	int npages = bp->b_bufsize >> PAGE_SHIFT;
    480  1.68.2.8     yamt 	struct vm_page *pgs[npages];
    481  1.68.2.8     yamt 	int i, error;
    482  1.68.2.8     yamt 	bool write;
    483  1.68.2.8     yamt 	UVMHIST_FUNC("uvm_aio_aiodone"); UVMHIST_CALLED(ubchist);
    484  1.68.2.8     yamt 	UVMHIST_LOG(ubchist, "bp %p", bp, 0,0,0);
    485  1.68.2.8     yamt 
    486  1.68.2.8     yamt 	error = bp->b_error;
    487  1.68.2.8     yamt 	write = (bp->b_flags & B_READ) == 0;
    488  1.68.2.8     yamt 	/* XXXUBC BC_NOCACHE is for swap pager, should be done differently */
    489  1.68.2.8     yamt 	if (write && !(bp->b_cflags & BC_NOCACHE) && bioopsp != NULL)
    490  1.68.2.8     yamt 		(*bioopsp->io_pageiodone)(bp);
    491  1.68.2.8     yamt 
    492  1.68.2.8     yamt 	for (i = 0; i < npages; i++) {
    493  1.68.2.8     yamt 		pgs[i] = uvm_pageratop((vaddr_t)bp->b_data + (i << PAGE_SHIFT));
    494  1.68.2.8     yamt 		UVMHIST_LOG(ubchist, "pgs[%d] = %p", i, pgs[i],0,0);
    495  1.68.2.8     yamt 	}
    496  1.68.2.8     yamt 	uvm_pagermapout((vaddr_t)bp->b_data, npages);
    497  1.68.2.8     yamt 
    498  1.68.2.8     yamt 	uvm_aio_aiodone_pages(pgs, npages, write, error);
    499  1.68.2.8     yamt 
    500  1.68.2.7     yamt 	if (write && (bp->b_cflags & BC_AGE) != 0) {
    501  1.68.2.7     yamt 		mutex_enter(bp->b_objlock);
    502      1.35      chs 		vwakeup(bp);
    503  1.68.2.7     yamt 		mutex_exit(bp->b_objlock);
    504      1.35      chs 	}
    505  1.68.2.1     yamt 	putiobuf(bp);
    506       1.1      mrg }
    507  1.68.2.1     yamt 
    508  1.68.2.1     yamt /*
    509  1.68.2.1     yamt  * uvm_pageratop: convert KVAs in the pager map back to their page
    510  1.68.2.1     yamt  * structures.
    511  1.68.2.1     yamt  */
    512  1.68.2.1     yamt 
    513  1.68.2.1     yamt struct vm_page *
    514  1.68.2.1     yamt uvm_pageratop(vaddr_t kva)
    515  1.68.2.1     yamt {
    516  1.68.2.1     yamt 	struct vm_page *pg;
    517  1.68.2.1     yamt 	paddr_t pa;
    518  1.68.2.3     yamt 	bool rv;
    519  1.68.2.1     yamt 
    520  1.68.2.1     yamt 	rv = pmap_extract(pmap_kernel(), kva, &pa);
    521  1.68.2.1     yamt 	KASSERT(rv);
    522  1.68.2.1     yamt 	pg = PHYS_TO_VM_PAGE(pa);
    523  1.68.2.1     yamt 	KASSERT(pg != NULL);
    524  1.68.2.1     yamt 	return (pg);
    525  1.68.2.1     yamt }
    526