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