Home | History | Annotate | Line # | Download | only in uvm
uvm_page_array.c revision 1.1.2.2
      1 /*	$NetBSD: uvm_page_array.c,v 1.1.2.2 2011/11/06 22:04:07 yamt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c)2011 YAMAMOTO Takashi,
      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  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: uvm_page_array.c,v 1.1.2.2 2011/11/06 22:04:07 yamt Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 
     35 #include <uvm/uvm_extern.h>
     36 #include <uvm/uvm_object.h>
     37 #include <uvm/uvm_page.h>
     38 #include <uvm/uvm_page_array.h>
     39 
     40 /*
     41  * uvm_page_array_init: initialize the array.
     42  */
     43 
     44 void
     45 uvm_page_array_init(struct uvm_page_array *ar)
     46 {
     47 
     48 	ar->ar_idx = ar->ar_npages = 0;
     49 }
     50 
     51 /*
     52  * uvm_page_array_fini: clean up the array.
     53  */
     54 
     55 void
     56 uvm_page_array_fini(struct uvm_page_array *ar)
     57 {
     58 
     59 	/*
     60 	 * currently nothing to do.
     61 	 */
     62 #if defined(DIAGNOSTIC)
     63 	/*
     64 	 * poison to trigger assertion in uvm_page_array_peek to
     65 	 * detect usage errors.
     66 	 */
     67 	ar->ar_npages = 1;
     68 	ar->ar_idx = 1000;
     69 #endif /* defined(DIAGNOSTIC) */
     70 }
     71 
     72 /*
     73  * uvm_page_array_clear: forget the cached pages and initialize the array.
     74  */
     75 
     76 void
     77 uvm_page_array_clear(struct uvm_page_array *ar)
     78 {
     79 
     80 	KASSERT(ar->ar_idx <= ar->ar_npages);
     81 	uvm_page_array_init(ar);
     82 }
     83 
     84 /*
     85  * uvm_page_array_peek: return the next cached page.
     86  */
     87 
     88 struct vm_page *
     89 uvm_page_array_peek(struct uvm_page_array *ar)
     90 {
     91 
     92 	KASSERT(ar->ar_idx <= ar->ar_npages);
     93 	if (ar->ar_idx == ar->ar_npages) {
     94 		return NULL;
     95 	}
     96 	return ar->ar_pages[ar->ar_idx];
     97 }
     98 
     99 /*
    100  * uvm_page_array_advance: advance the array to the next cached page
    101  */
    102 
    103 void
    104 uvm_page_array_advance(struct uvm_page_array *ar)
    105 {
    106 
    107 	KASSERT(ar->ar_idx <= ar->ar_npages);
    108 	ar->ar_idx++;
    109 	KASSERT(ar->ar_idx <= ar->ar_npages);
    110 }
    111 
    112 /*
    113  * uvm_page_array_fill: lookup pages and keep them cached.
    114  *
    115  * return 0 on success.  in that case, cache the result in the array
    116  * so that they will be picked by later uvm_page_array_peek.
    117  *
    118  * return ENOENT if no pages are found.
    119  *
    120  * called with object lock held.
    121  */
    122 
    123 int
    124 uvm_page_array_fill(struct uvm_page_array *ar, struct uvm_object *uobj,
    125     voff_t off, bool dirtyonly)
    126 {
    127 	unsigned int npages;
    128 #if defined(DEBUG)
    129 	unsigned int i;
    130 #endif /* defined(DEBUG) */
    131 	const unsigned int maxpages = __arraycount(ar->ar_pages);
    132 
    133 	KASSERT(mutex_owned(uobj->vmobjlock));
    134 	KASSERT(uvm_page_array_peek(ar) == NULL);
    135 	if (dirtyonly) {
    136 		npages = radix_tree_gang_lookup_tagged_node(
    137 		    &uobj->uo_pages, off >> PAGE_SHIFT,
    138 		    (void **)ar->ar_pages, maxpages,
    139 		    UVM_PAGE_DIRTY_TAG);
    140 	} else {
    141 		npages = radix_tree_gang_lookup_node(
    142 		    &uobj->uo_pages, off >> PAGE_SHIFT,
    143 		    (void **)ar->ar_pages, maxpages);
    144 	}
    145 	if (npages == 0) {
    146 		uvm_page_array_clear(ar);
    147 		return ENOENT;
    148 	}
    149 	KASSERT(npages <= maxpages);
    150 	ar->ar_npages = npages;
    151 	ar->ar_idx = 0;
    152 #if defined(DEBUG)
    153 	for (i = 0; i < ar->ar_npages; i++) {
    154 		struct vm_page * const pg = ar->ar_pages[i];
    155 
    156 		KASSERT(pg != NULL);
    157 		KASSERT(pg->uobject == uobj);
    158 		KASSERT(pg->offset >= off);
    159 		KASSERT(i == 0 || pg->offset > ar->ar_pages[i - 1]->offset);
    160 	}
    161 #endif /* defined(DEBUG) */
    162 	return 0;
    163 }
    164 
    165 /*
    166  * uvm_page_array_fill_and_peek:
    167  * same as uvm_page_array_peek except that, if the array is empty, try to fill
    168  * it first.
    169  */
    170 
    171 struct vm_page *
    172 uvm_page_array_fill_and_peek(struct uvm_page_array *a, struct uvm_object *uobj,
    173     voff_t off, bool dirtyonly)
    174 {
    175 	struct vm_page *pg;
    176 	int error;
    177 
    178 	pg = uvm_page_array_peek(a);
    179 	if (pg != NULL) {
    180 		return pg;
    181 	}
    182 	error = uvm_page_array_fill(a, uobj, off, dirtyonly);
    183 	if (error != 0) {
    184 		return NULL;
    185 	}
    186 	pg = uvm_page_array_peek(a);
    187 	KASSERT(pg != NULL);
    188 	return pg;
    189 }
    190