Home | History | Annotate | Line # | Download | only in uvm
uvm_object.c revision 1.2.4.2
      1  1.2.4.2  ad /*	$NetBSD: uvm_object.c,v 1.2.4.2 2006/11/18 21:39:50 ad Exp $	*/
      2  1.2.4.2  ad 
      3  1.2.4.2  ad /*
      4  1.2.4.2  ad  * Copyright (c) 2006 The NetBSD Foundation, Inc.
      5  1.2.4.2  ad  * All rights reserved.
      6  1.2.4.2  ad  *
      7  1.2.4.2  ad  * Redistribution and use in source and binary forms, with or without
      8  1.2.4.2  ad  * modification, are permitted provided that the following conditions
      9  1.2.4.2  ad  * are met:
     10  1.2.4.2  ad  * 1. Redistributions of source code must retain the above copyright
     11  1.2.4.2  ad  *    notice, this list of conditions and the following disclaimer.
     12  1.2.4.2  ad  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.2.4.2  ad  *    notice, this list of conditions and the following disclaimer in the
     14  1.2.4.2  ad  *    documentation and/or other materials provided with the distribution.
     15  1.2.4.2  ad  * 3. All advertising materials mentioning features or use of this software
     16  1.2.4.2  ad  *    must display the following acknowledgement:
     17  1.2.4.2  ad  *      This product includes software developed by the NetBSD
     18  1.2.4.2  ad  *      Foundation, Inc. and its contributors.
     19  1.2.4.2  ad  * 4. Neither the name of The NetBSD Foundation nor the names of its
     20  1.2.4.2  ad  *    contributors may be used to endorse or promote products derived
     21  1.2.4.2  ad  *    from this software without specific prior written permission.
     22  1.2.4.2  ad  *
     23  1.2.4.2  ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     24  1.2.4.2  ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  1.2.4.2  ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  1.2.4.2  ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     27  1.2.4.2  ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  1.2.4.2  ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  1.2.4.2  ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  1.2.4.2  ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  1.2.4.2  ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  1.2.4.2  ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  1.2.4.2  ad  * POSSIBILITY OF SUCH DAMAGE.
     34  1.2.4.2  ad  */
     35  1.2.4.2  ad 
     36  1.2.4.2  ad /*
     37  1.2.4.2  ad  * uvm_object.c: operate with memory objects
     38  1.2.4.2  ad  *
     39  1.2.4.2  ad  * TODO:
     40  1.2.4.2  ad  *  1. Support PG_RELEASED-using objects
     41  1.2.4.2  ad  *
     42  1.2.4.2  ad  */
     43  1.2.4.2  ad 
     44  1.2.4.2  ad #include <sys/cdefs.h>
     45  1.2.4.2  ad __KERNEL_RCSID(0, "$NetBSD: uvm_object.c,v 1.2.4.2 2006/11/18 21:39:50 ad Exp $");
     46  1.2.4.2  ad 
     47  1.2.4.2  ad #include "opt_uvmhist.h"
     48  1.2.4.2  ad 
     49  1.2.4.2  ad #include <sys/param.h>
     50  1.2.4.2  ad #include <sys/lock.h>
     51  1.2.4.2  ad 
     52  1.2.4.2  ad #include <uvm/uvm.h>
     53  1.2.4.2  ad 
     54  1.2.4.2  ad /* We will fetch this page count per step */
     55  1.2.4.2  ad #define	FETCH_PAGECOUNT	16
     56  1.2.4.2  ad 
     57  1.2.4.2  ad /*
     58  1.2.4.2  ad  * uobj_wirepages: wire the pages of entire uobj
     59  1.2.4.2  ad  *
     60  1.2.4.2  ad  * => NOTE: this function should only be used for types of objects
     61  1.2.4.2  ad  *  where PG_RELEASED flag is never set (aobj objects)
     62  1.2.4.2  ad  * => caller must pass page-aligned start and end values
     63  1.2.4.2  ad  */
     64  1.2.4.2  ad 
     65  1.2.4.2  ad int
     66  1.2.4.2  ad uobj_wirepages(struct uvm_object *uobj, off_t start, off_t end)
     67  1.2.4.2  ad {
     68  1.2.4.2  ad 	int i, npages, error;
     69  1.2.4.2  ad 	struct vm_page *pgs[FETCH_PAGECOUNT], *pg = NULL;
     70  1.2.4.2  ad 	off_t offset = start, left;
     71  1.2.4.2  ad 
     72  1.2.4.2  ad 	left = (end - start) >> PAGE_SHIFT;
     73  1.2.4.2  ad 
     74  1.2.4.2  ad 	simple_lock(&uobj->vmobjlock);
     75  1.2.4.2  ad 	while (left) {
     76  1.2.4.2  ad 
     77  1.2.4.2  ad 		npages = MIN(FETCH_PAGECOUNT, left);
     78  1.2.4.2  ad 
     79  1.2.4.2  ad 		/* Get the pages */
     80  1.2.4.2  ad 		memset(pgs, 0, sizeof(pgs));
     81  1.2.4.2  ad 		error = (*uobj->pgops->pgo_get)(uobj, offset, pgs, &npages, 0,
     82  1.2.4.2  ad 			VM_PROT_READ | VM_PROT_WRITE, UVM_ADV_SEQUENTIAL,
     83  1.2.4.2  ad 			PGO_ALLPAGES | PGO_SYNCIO);
     84  1.2.4.2  ad 
     85  1.2.4.2  ad 		if (error)
     86  1.2.4.2  ad 			goto error;
     87  1.2.4.2  ad 
     88  1.2.4.2  ad 		simple_lock(&uobj->vmobjlock);
     89  1.2.4.2  ad 		for (i = 0; i < npages; i++) {
     90  1.2.4.2  ad 
     91  1.2.4.2  ad 			KASSERT(pgs[i] != NULL);
     92  1.2.4.2  ad 			KASSERT(!(pgs[i]->flags & PG_RELEASED));
     93  1.2.4.2  ad 
     94  1.2.4.2  ad 			/*
     95  1.2.4.2  ad 			 * Loan break
     96  1.2.4.2  ad 			 */
     97  1.2.4.2  ad 			if (pgs[i]->loan_count) {
     98  1.2.4.2  ad 				while (pgs[i]->loan_count) {
     99  1.2.4.2  ad 					pg = uvm_loanbreak(pgs[i]);
    100  1.2.4.2  ad 					if (!pg) {
    101  1.2.4.2  ad 						simple_unlock(&uobj->vmobjlock);
    102  1.2.4.2  ad 						uvm_wait("uobjwirepg");
    103  1.2.4.2  ad 						simple_lock(&uobj->vmobjlock);
    104  1.2.4.2  ad 						continue;
    105  1.2.4.2  ad 					}
    106  1.2.4.2  ad 				}
    107  1.2.4.2  ad 				pgs[i] = pg;
    108  1.2.4.2  ad 			}
    109  1.2.4.2  ad 
    110  1.2.4.2  ad 			if (pgs[i]->pqflags & PQ_AOBJ) {
    111  1.2.4.2  ad 				pgs[i]->flags &= ~(PG_CLEAN);
    112  1.2.4.2  ad 				uao_dropswap(uobj, i);
    113  1.2.4.2  ad 			}
    114  1.2.4.2  ad 		}
    115  1.2.4.2  ad 
    116  1.2.4.2  ad 		/* Wire the pages */
    117  1.2.4.2  ad 		uvm_lock_pageq();
    118  1.2.4.2  ad 		for (i = 0; i < npages; i++) {
    119  1.2.4.2  ad 			uvm_pagewire(pgs[i]);
    120  1.2.4.2  ad 		}
    121  1.2.4.2  ad 		uvm_unlock_pageq();
    122  1.2.4.2  ad 
    123  1.2.4.2  ad 		/* Unbusy the pages */
    124  1.2.4.2  ad 		uvm_page_unbusy(pgs, npages);
    125  1.2.4.2  ad 
    126  1.2.4.2  ad 		left -= npages;
    127  1.2.4.2  ad 		offset += npages << PAGE_SHIFT;
    128  1.2.4.2  ad 	}
    129  1.2.4.2  ad 	simple_unlock(&uobj->vmobjlock);
    130  1.2.4.2  ad 
    131  1.2.4.2  ad 	return 0;
    132  1.2.4.2  ad 
    133  1.2.4.2  ad error:
    134  1.2.4.2  ad 	/* Unwire the pages which has been wired */
    135  1.2.4.2  ad 	uobj_unwirepages(uobj, start, offset);
    136  1.2.4.2  ad 
    137  1.2.4.2  ad 	return error;
    138  1.2.4.2  ad }
    139  1.2.4.2  ad 
    140  1.2.4.2  ad /*
    141  1.2.4.2  ad  * uobj_unwirepages: unwire the pages of entire uobj
    142  1.2.4.2  ad  *
    143  1.2.4.2  ad  * => NOTE: this function should only be used for types of objects
    144  1.2.4.2  ad  *  where PG_RELEASED flag is never set
    145  1.2.4.2  ad  * => caller must pass page-aligned start and end values
    146  1.2.4.2  ad  */
    147  1.2.4.2  ad 
    148  1.2.4.2  ad void
    149  1.2.4.2  ad uobj_unwirepages(struct uvm_object *uobj, off_t start, off_t end)
    150  1.2.4.2  ad {
    151  1.2.4.2  ad 	struct vm_page *pg;
    152  1.2.4.2  ad 	off_t offset;
    153  1.2.4.2  ad 
    154  1.2.4.2  ad 	simple_lock(&uobj->vmobjlock);
    155  1.2.4.2  ad 	uvm_lock_pageq();
    156  1.2.4.2  ad 	for (offset = start; offset < end; offset += PAGE_SIZE) {
    157  1.2.4.2  ad 		pg = uvm_pagelookup(uobj, offset);
    158  1.2.4.2  ad 
    159  1.2.4.2  ad 		KASSERT(pg != NULL);
    160  1.2.4.2  ad 		KASSERT(!(pg->flags & PG_RELEASED));
    161  1.2.4.2  ad 
    162  1.2.4.2  ad 		uvm_pageunwire(pg);
    163  1.2.4.2  ad 	}
    164  1.2.4.2  ad 	uvm_unlock_pageq();
    165  1.2.4.2  ad 	simple_unlock(&uobj->vmobjlock);
    166  1.2.4.2  ad }
    167