Home | History | Annotate | Line # | Download | only in uvm
uvm_object.c revision 1.7.4.5
      1 /*	$NetBSD: uvm_object.c,v 1.7.4.5 2011/03/05 20:56:37 rmind Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2006, 2010 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * uvm_object.c: operate with memory objects
     34  *
     35  * TODO:
     36  *  1. Support PG_RELEASED-using objects
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: uvm_object.c,v 1.7.4.5 2011/03/05 20:56:37 rmind Exp $");
     41 
     42 #include "opt_ddb.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/mutex.h>
     46 #include <sys/queue.h>
     47 #include <sys/rbtree.h>
     48 
     49 #include <uvm/uvm.h>
     50 #include <uvm/uvm_ddb.h>
     51 
     52 /* Page count to fetch per single step. */
     53 #define	FETCH_PAGECOUNT			16
     54 
     55 /*
     56  * uvm_obj_init: initialize UVM memory object.
     57  */
     58 void
     59 uvm_obj_init(struct uvm_object *uo, const struct uvm_pagerops *ops,
     60     kmutex_t *lockptr, u_int refs)
     61 {
     62 
     63 	if (lockptr == NULL) {
     64 		uo->vmobjlock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
     65 	} else {
     66 		uo->vmobjlock = lockptr;
     67 		mutex_init(uo->vmobjlock, MUTEX_DEFAULT, IPL_NONE);
     68 	}
     69 	uo->pgops = ops;
     70 	TAILQ_INIT(&uo->memq);
     71 	LIST_INIT(&uo->uo_ubc);
     72 	uo->uo_npages = 0;
     73 	uo->uo_refs = refs;
     74 	rb_tree_init(&uo->rb_tree, &uvm_page_tree_ops);
     75 }
     76 
     77 /*
     78  * uvm_obj_destroy: destroy UVM memory object.
     79  */
     80 void
     81 uvm_obj_destroy(struct uvm_object *uo, kmutex_t *lockptr)
     82 {
     83 #ifdef DIAGNOSTIC
     84 	void *tmp = NULL;
     85 	KASSERT(rb_tree_find_node_geq(&uo->rb_tree, &tmp) == NULL);
     86 #endif
     87 	/* Purge any UBC entries with this object. */
     88 	if (__predict_false(!LIST_EMPTY(&uo->uo_ubc))) {
     89 		ubc_purge(uo);
     90 	}
     91 	/* Finally, safe to destory the lock. */
     92 	if (lockptr) {
     93 		KASSERT(uo->vmobjlock == lockptr);
     94 		mutex_destroy(uo->vmobjlock);
     95 	} else {
     96 		mutex_obj_free(uo->vmobjlock);
     97 	}
     98 }
     99 
    100 /*
    101  * uobj_wirepages: wire the pages of entire uobj
    102  *
    103  * => NOTE: this function should only be used for types of objects
    104  *  where PG_RELEASED flag is never set (aobj objects)
    105  * => caller must pass page-aligned start and end values
    106  */
    107 
    108 int
    109 uvm_obj_wirepages(struct uvm_object *uobj, off_t start, off_t end)
    110 {
    111 	int i, npages, error;
    112 	struct vm_page *pgs[FETCH_PAGECOUNT], *pg = NULL;
    113 	off_t offset = start, left;
    114 
    115 	left = (end - start) >> PAGE_SHIFT;
    116 
    117 	mutex_enter(uobj->vmobjlock);
    118 	while (left) {
    119 
    120 		npages = MIN(FETCH_PAGECOUNT, left);
    121 
    122 		/* Get the pages */
    123 		memset(pgs, 0, sizeof(pgs));
    124 		error = (*uobj->pgops->pgo_get)(uobj, offset, pgs, &npages, 0,
    125 			VM_PROT_READ | VM_PROT_WRITE, UVM_ADV_SEQUENTIAL,
    126 			PGO_ALLPAGES | PGO_SYNCIO);
    127 
    128 		if (error)
    129 			goto error;
    130 
    131 		mutex_enter(uobj->vmobjlock);
    132 		for (i = 0; i < npages; i++) {
    133 
    134 			KASSERT(pgs[i] != NULL);
    135 			KASSERT(!(pgs[i]->flags & PG_RELEASED));
    136 
    137 			/*
    138 			 * Loan break
    139 			 */
    140 			if (pgs[i]->loan_count) {
    141 				while (pgs[i]->loan_count) {
    142 					pg = uvm_loanbreak(pgs[i]);
    143 					if (!pg) {
    144 						mutex_exit(uobj->vmobjlock);
    145 						uvm_wait("uobjwirepg");
    146 						mutex_enter(uobj->vmobjlock);
    147 						continue;
    148 					}
    149 				}
    150 				pgs[i] = pg;
    151 			}
    152 
    153 			if (pgs[i]->pqflags & PQ_AOBJ) {
    154 				pgs[i]->flags &= ~(PG_CLEAN);
    155 				uao_dropswap(uobj, i);
    156 			}
    157 		}
    158 
    159 		/* Wire the pages */
    160 		mutex_enter(&uvm_pageqlock);
    161 		for (i = 0; i < npages; i++) {
    162 			uvm_pagewire(pgs[i]);
    163 		}
    164 		mutex_exit(&uvm_pageqlock);
    165 
    166 		/* Unbusy the pages */
    167 		uvm_page_unbusy(pgs, npages);
    168 
    169 		left -= npages;
    170 		offset += npages << PAGE_SHIFT;
    171 	}
    172 	mutex_exit(uobj->vmobjlock);
    173 
    174 	return 0;
    175 
    176 error:
    177 	/* Unwire the pages which has been wired */
    178 	uvm_obj_unwirepages(uobj, start, offset);
    179 
    180 	return error;
    181 }
    182 
    183 /*
    184  * uobj_unwirepages: unwire the pages of entire uobj
    185  *
    186  * => NOTE: this function should only be used for types of objects
    187  *  where PG_RELEASED flag is never set
    188  * => caller must pass page-aligned start and end values
    189  */
    190 
    191 void
    192 uvm_obj_unwirepages(struct uvm_object *uobj, off_t start, off_t end)
    193 {
    194 	struct vm_page *pg;
    195 	off_t offset;
    196 
    197 	mutex_enter(uobj->vmobjlock);
    198 	mutex_enter(&uvm_pageqlock);
    199 	for (offset = start; offset < end; offset += PAGE_SIZE) {
    200 		pg = uvm_pagelookup(uobj, offset);
    201 
    202 		KASSERT(pg != NULL);
    203 		KASSERT(!(pg->flags & PG_RELEASED));
    204 
    205 		uvm_pageunwire(pg);
    206 	}
    207 	mutex_exit(&uvm_pageqlock);
    208 	mutex_exit(uobj->vmobjlock);
    209 }
    210 
    211 #if defined(DDB) || defined(DEBUGPRINT)
    212 
    213 /*
    214  * uvm_object_printit: actually prints the object
    215  */
    216 void
    217 uvm_object_printit(struct uvm_object *uobj, bool full,
    218     void (*pr)(const char *, ...))
    219 {
    220 	struct vm_page *pg;
    221 	int cnt = 0;
    222 
    223 	(*pr)("OBJECT %p: locked=%d, pgops=%p, npages=%d, ",
    224 	    uobj, mutex_owned(uobj->vmobjlock), uobj->pgops, uobj->uo_npages);
    225 	if (UVM_OBJ_IS_KERN_OBJECT(uobj))
    226 		(*pr)("refs=<SYSTEM>\n");
    227 	else
    228 		(*pr)("refs=%d\n", uobj->uo_refs);
    229 
    230 	if (!full) {
    231 		return;
    232 	}
    233 	(*pr)("  PAGES <pg,offset>:\n  ");
    234 	TAILQ_FOREACH(pg, &uobj->memq, listq.queue) {
    235 		cnt++;
    236 		(*pr)("<%p,0x%llx> ", pg, (long long)pg->offset);
    237 		if ((cnt % 3) == 0) {
    238 			(*pr)("\n  ");
    239 		}
    240 	}
    241 	if ((cnt % 3) != 0) {
    242 		(*pr)("\n");
    243 	}
    244 }
    245 
    246 #endif /* DDB || DEBUGPRINT */
    247