Home | History | Annotate | Line # | Download | only in uvm
uvm_object.c revision 1.7.10.1
      1  1.7.10.1   cherry /*	$NetBSD: uvm_object.c,v 1.7.10.1 2011/06/23 14:20:36 cherry Exp $	*/
      2       1.1     yamt 
      3       1.1     yamt /*
      4  1.7.10.1   cherry  * Copyright (c) 2006, 2010 The NetBSD Foundation, Inc.
      5       1.1     yamt  * All rights reserved.
      6       1.1     yamt  *
      7       1.3    rmind  * This code is derived from software contributed to The NetBSD Foundation
      8       1.3    rmind  * by Mindaugas Rasiukevicius.
      9       1.3    rmind  *
     10       1.1     yamt  * Redistribution and use in source and binary forms, with or without
     11       1.1     yamt  * modification, are permitted provided that the following conditions
     12       1.1     yamt  * are met:
     13       1.1     yamt  * 1. Redistributions of source code must retain the above copyright
     14       1.1     yamt  *    notice, this list of conditions and the following disclaimer.
     15       1.1     yamt  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1     yamt  *    notice, this list of conditions and the following disclaimer in the
     17       1.1     yamt  *    documentation and/or other materials provided with the distribution.
     18       1.1     yamt  *
     19       1.1     yamt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1     yamt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1     yamt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1     yamt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1     yamt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1     yamt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1     yamt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1     yamt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1     yamt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1     yamt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1     yamt  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1     yamt  */
     31       1.1     yamt 
     32       1.1     yamt /*
     33       1.1     yamt  * uvm_object.c: operate with memory objects
     34       1.1     yamt  *
     35       1.1     yamt  * TODO:
     36       1.1     yamt  *  1. Support PG_RELEASED-using objects
     37       1.1     yamt  */
     38       1.1     yamt 
     39       1.1     yamt #include <sys/cdefs.h>
     40  1.7.10.1   cherry __KERNEL_RCSID(0, "$NetBSD: uvm_object.c,v 1.7.10.1 2011/06/23 14:20:36 cherry Exp $");
     41       1.1     yamt 
     42       1.7  thorpej #include "opt_ddb.h"
     43       1.1     yamt 
     44       1.1     yamt #include <sys/param.h>
     45  1.7.10.1   cherry #include <sys/mutex.h>
     46  1.7.10.1   cherry #include <sys/queue.h>
     47  1.7.10.1   cherry #include <sys/rbtree.h>
     48       1.1     yamt 
     49       1.1     yamt #include <uvm/uvm.h>
     50       1.7  thorpej #include <uvm/uvm_ddb.h>
     51       1.1     yamt 
     52  1.7.10.1   cherry /* Page count to fetch per single step. */
     53  1.7.10.1   cherry #define	FETCH_PAGECOUNT			16
     54  1.7.10.1   cherry 
     55  1.7.10.1   cherry /*
     56  1.7.10.1   cherry  * uvm_obj_init: initialize UVM memory object.
     57  1.7.10.1   cherry  */
     58  1.7.10.1   cherry void
     59  1.7.10.1   cherry uvm_obj_init(struct uvm_object *uo, const struct uvm_pagerops *ops,
     60  1.7.10.1   cherry     bool alock, u_int refs)
     61  1.7.10.1   cherry {
     62  1.7.10.1   cherry 
     63  1.7.10.1   cherry 	if (alock) {
     64  1.7.10.1   cherry 		/* Allocate and assign a lock. */
     65  1.7.10.1   cherry 		uo->vmobjlock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
     66  1.7.10.1   cherry 	} else {
     67  1.7.10.1   cherry 		/* The lock will need to be set via uvm_obj_setlock(). */
     68  1.7.10.1   cherry 		uo->vmobjlock = NULL;
     69  1.7.10.1   cherry 	}
     70  1.7.10.1   cherry 	uo->pgops = ops;
     71  1.7.10.1   cherry 	TAILQ_INIT(&uo->memq);
     72  1.7.10.1   cherry 	LIST_INIT(&uo->uo_ubc);
     73  1.7.10.1   cherry 	uo->uo_npages = 0;
     74  1.7.10.1   cherry 	uo->uo_refs = refs;
     75  1.7.10.1   cherry 	rb_tree_init(&uo->rb_tree, &uvm_page_tree_ops);
     76  1.7.10.1   cherry }
     77  1.7.10.1   cherry 
     78  1.7.10.1   cherry /*
     79  1.7.10.1   cherry  * uvm_obj_destroy: destroy UVM memory object.
     80  1.7.10.1   cherry  */
     81  1.7.10.1   cherry void
     82  1.7.10.1   cherry uvm_obj_destroy(struct uvm_object *uo, bool dlock)
     83  1.7.10.1   cherry {
     84  1.7.10.1   cherry 
     85  1.7.10.1   cherry 	KASSERT(rb_tree_iterate(&uo->rb_tree, NULL, RB_DIR_LEFT) == NULL);
     86  1.7.10.1   cherry 
     87  1.7.10.1   cherry 	/* Purge any UBC entries associated with this object. */
     88  1.7.10.1   cherry 	ubc_purge(uo);
     89  1.7.10.1   cherry 
     90  1.7.10.1   cherry 	/* Destroy the lock, if requested. */
     91  1.7.10.1   cherry 	if (dlock) {
     92  1.7.10.1   cherry 		mutex_obj_free(uo->vmobjlock);
     93  1.7.10.1   cherry 	}
     94  1.7.10.1   cherry }
     95  1.7.10.1   cherry 
     96  1.7.10.1   cherry /*
     97  1.7.10.1   cherry  * uvm_obj_setlock: assign a vmobjlock to the UVM object.
     98  1.7.10.1   cherry  *
     99  1.7.10.1   cherry  * => Caller is responsible to ensure that UVM objects is not use.
    100  1.7.10.1   cherry  * => Only dynamic lock may be previously set.  We drop the reference then.
    101  1.7.10.1   cherry  */
    102  1.7.10.1   cherry void
    103  1.7.10.1   cherry uvm_obj_setlock(struct uvm_object *uo, kmutex_t *lockptr)
    104  1.7.10.1   cherry {
    105  1.7.10.1   cherry 	kmutex_t *olockptr = uo->vmobjlock;
    106  1.7.10.1   cherry 
    107  1.7.10.1   cherry 	if (olockptr) {
    108  1.7.10.1   cherry 		/* Drop the reference on the old lock. */
    109  1.7.10.1   cherry 		mutex_obj_free(olockptr);
    110  1.7.10.1   cherry 	}
    111  1.7.10.1   cherry 	if (lockptr == NULL) {
    112  1.7.10.1   cherry 		/* If new lock is not passed - allocate default one. */
    113  1.7.10.1   cherry 		lockptr = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    114  1.7.10.1   cherry 	}
    115  1.7.10.1   cherry 	uo->vmobjlock = lockptr;
    116  1.7.10.1   cherry }
    117       1.1     yamt 
    118       1.1     yamt /*
    119  1.7.10.1   cherry  * uvm_obj_wirepages: wire the pages of entire UVM object.
    120       1.1     yamt  *
    121       1.1     yamt  * => NOTE: this function should only be used for types of objects
    122       1.1     yamt  *  where PG_RELEASED flag is never set (aobj objects)
    123       1.1     yamt  * => caller must pass page-aligned start and end values
    124       1.1     yamt  */
    125       1.1     yamt int
    126  1.7.10.1   cherry uvm_obj_wirepages(struct uvm_object *uobj, off_t start, off_t end)
    127       1.1     yamt {
    128       1.1     yamt 	int i, npages, error;
    129       1.1     yamt 	struct vm_page *pgs[FETCH_PAGECOUNT], *pg = NULL;
    130       1.1     yamt 	off_t offset = start, left;
    131       1.1     yamt 
    132       1.1     yamt 	left = (end - start) >> PAGE_SHIFT;
    133       1.1     yamt 
    134  1.7.10.1   cherry 	mutex_enter(uobj->vmobjlock);
    135       1.1     yamt 	while (left) {
    136       1.1     yamt 
    137       1.1     yamt 		npages = MIN(FETCH_PAGECOUNT, left);
    138       1.1     yamt 
    139       1.1     yamt 		/* Get the pages */
    140       1.1     yamt 		memset(pgs, 0, sizeof(pgs));
    141       1.1     yamt 		error = (*uobj->pgops->pgo_get)(uobj, offset, pgs, &npages, 0,
    142       1.1     yamt 			VM_PROT_READ | VM_PROT_WRITE, UVM_ADV_SEQUENTIAL,
    143       1.1     yamt 			PGO_ALLPAGES | PGO_SYNCIO);
    144       1.1     yamt 
    145       1.1     yamt 		if (error)
    146       1.1     yamt 			goto error;
    147       1.1     yamt 
    148  1.7.10.1   cherry 		mutex_enter(uobj->vmobjlock);
    149       1.1     yamt 		for (i = 0; i < npages; i++) {
    150       1.1     yamt 
    151       1.1     yamt 			KASSERT(pgs[i] != NULL);
    152       1.1     yamt 			KASSERT(!(pgs[i]->flags & PG_RELEASED));
    153       1.1     yamt 
    154       1.1     yamt 			/*
    155       1.1     yamt 			 * Loan break
    156       1.1     yamt 			 */
    157       1.1     yamt 			if (pgs[i]->loan_count) {
    158       1.1     yamt 				while (pgs[i]->loan_count) {
    159       1.1     yamt 					pg = uvm_loanbreak(pgs[i]);
    160       1.1     yamt 					if (!pg) {
    161  1.7.10.1   cherry 						mutex_exit(uobj->vmobjlock);
    162       1.1     yamt 						uvm_wait("uobjwirepg");
    163  1.7.10.1   cherry 						mutex_enter(uobj->vmobjlock);
    164       1.1     yamt 						continue;
    165       1.1     yamt 					}
    166       1.1     yamt 				}
    167       1.1     yamt 				pgs[i] = pg;
    168       1.1     yamt 			}
    169       1.1     yamt 
    170       1.1     yamt 			if (pgs[i]->pqflags & PQ_AOBJ) {
    171       1.1     yamt 				pgs[i]->flags &= ~(PG_CLEAN);
    172       1.1     yamt 				uao_dropswap(uobj, i);
    173       1.1     yamt 			}
    174       1.1     yamt 		}
    175       1.1     yamt 
    176       1.1     yamt 		/* Wire the pages */
    177       1.4       ad 		mutex_enter(&uvm_pageqlock);
    178       1.1     yamt 		for (i = 0; i < npages; i++) {
    179       1.1     yamt 			uvm_pagewire(pgs[i]);
    180       1.1     yamt 		}
    181       1.4       ad 		mutex_exit(&uvm_pageqlock);
    182       1.1     yamt 
    183       1.1     yamt 		/* Unbusy the pages */
    184       1.1     yamt 		uvm_page_unbusy(pgs, npages);
    185       1.1     yamt 
    186       1.1     yamt 		left -= npages;
    187       1.1     yamt 		offset += npages << PAGE_SHIFT;
    188       1.1     yamt 	}
    189  1.7.10.1   cherry 	mutex_exit(uobj->vmobjlock);
    190       1.1     yamt 
    191       1.1     yamt 	return 0;
    192       1.1     yamt 
    193       1.1     yamt error:
    194       1.1     yamt 	/* Unwire the pages which has been wired */
    195  1.7.10.1   cherry 	uvm_obj_unwirepages(uobj, start, offset);
    196       1.1     yamt 
    197       1.1     yamt 	return error;
    198       1.1     yamt }
    199       1.1     yamt 
    200       1.1     yamt /*
    201  1.7.10.1   cherry  * uvm_obj_unwirepages: unwire the pages of entire UVM object.
    202       1.1     yamt  *
    203       1.1     yamt  * => NOTE: this function should only be used for types of objects
    204       1.1     yamt  *  where PG_RELEASED flag is never set
    205       1.1     yamt  * => caller must pass page-aligned start and end values
    206       1.1     yamt  */
    207       1.1     yamt void
    208  1.7.10.1   cherry uvm_obj_unwirepages(struct uvm_object *uobj, off_t start, off_t end)
    209       1.1     yamt {
    210       1.1     yamt 	struct vm_page *pg;
    211       1.1     yamt 	off_t offset;
    212       1.1     yamt 
    213  1.7.10.1   cherry 	mutex_enter(uobj->vmobjlock);
    214       1.4       ad 	mutex_enter(&uvm_pageqlock);
    215       1.1     yamt 	for (offset = start; offset < end; offset += PAGE_SIZE) {
    216       1.1     yamt 		pg = uvm_pagelookup(uobj, offset);
    217       1.1     yamt 
    218       1.1     yamt 		KASSERT(pg != NULL);
    219       1.1     yamt 		KASSERT(!(pg->flags & PG_RELEASED));
    220       1.1     yamt 
    221       1.1     yamt 		uvm_pageunwire(pg);
    222       1.1     yamt 	}
    223       1.4       ad 	mutex_exit(&uvm_pageqlock);
    224  1.7.10.1   cherry 	mutex_exit(uobj->vmobjlock);
    225       1.1     yamt }
    226       1.7  thorpej 
    227  1.7.10.1   cherry #if (defined(DDB) || defined(DEBUGPRINT)) && !defined(_RUMPKERNEL)
    228       1.7  thorpej 
    229       1.7  thorpej /*
    230       1.7  thorpej  * uvm_object_printit: actually prints the object
    231       1.7  thorpej  */
    232       1.7  thorpej void
    233       1.7  thorpej uvm_object_printit(struct uvm_object *uobj, bool full,
    234       1.7  thorpej     void (*pr)(const char *, ...))
    235       1.7  thorpej {
    236       1.7  thorpej 	struct vm_page *pg;
    237       1.7  thorpej 	int cnt = 0;
    238       1.7  thorpej 
    239       1.7  thorpej 	(*pr)("OBJECT %p: locked=%d, pgops=%p, npages=%d, ",
    240  1.7.10.1   cherry 	    uobj, mutex_owned(uobj->vmobjlock), uobj->pgops, uobj->uo_npages);
    241       1.7  thorpej 	if (UVM_OBJ_IS_KERN_OBJECT(uobj))
    242       1.7  thorpej 		(*pr)("refs=<SYSTEM>\n");
    243       1.7  thorpej 	else
    244       1.7  thorpej 		(*pr)("refs=%d\n", uobj->uo_refs);
    245       1.7  thorpej 
    246       1.7  thorpej 	if (!full) {
    247       1.7  thorpej 		return;
    248       1.7  thorpej 	}
    249       1.7  thorpej 	(*pr)("  PAGES <pg,offset>:\n  ");
    250       1.7  thorpej 	TAILQ_FOREACH(pg, &uobj->memq, listq.queue) {
    251       1.7  thorpej 		cnt++;
    252       1.7  thorpej 		(*pr)("<%p,0x%llx> ", pg, (long long)pg->offset);
    253       1.7  thorpej 		if ((cnt % 3) == 0) {
    254       1.7  thorpej 			(*pr)("\n  ");
    255       1.7  thorpej 		}
    256       1.7  thorpej 	}
    257       1.7  thorpej 	if ((cnt % 3) != 0) {
    258       1.7  thorpej 		(*pr)("\n");
    259       1.7  thorpej 	}
    260       1.7  thorpej }
    261       1.7  thorpej 
    262       1.7  thorpej #endif /* DDB || DEBUGPRINT */
    263