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