uvm_page_array.c revision 1.1.2.5 1 /* $NetBSD: uvm_page_array.c,v 1.1.2.5 2012/04/18 13:40:44 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.5 2012/04/18 13:40:44 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 * nwant is a number of pages to fetch. a caller should consider it a hint.
119 * nwant == 0 means a caller have no specific idea.
120 *
121 * return ENOENT if no pages are found.
122 *
123 * called with object lock held.
124 */
125
126 int
127 uvm_page_array_fill(struct uvm_page_array *ar, struct uvm_object *uobj,
128 voff_t off, unsigned int nwant, unsigned int flags)
129 {
130 unsigned int npages;
131 #if defined(DEBUG)
132 unsigned int i;
133 #endif /* defined(DEBUG) */
134 unsigned int maxpages = __arraycount(ar->ar_pages);
135 const bool dense = (flags & UVM_PAGE_ARRAY_FILL_DENSE) != 0;
136 const bool backward = (flags & UVM_PAGE_ARRAY_FILL_BACKWARD) != 0;
137
138 if (nwant != 0 && nwant < maxpages) {
139 maxpages = nwant;
140 }
141 #if 0 /* called from DDB for "show obj/f" without lock */
142 KASSERT(mutex_owned(uobj->vmobjlock));
143 #endif
144 KASSERT(uvm_page_array_peek(ar) == NULL);
145 if ((flags & UVM_PAGE_ARRAY_FILL_DIRTYONLY) != 0) {
146 npages =
147 (backward ? radix_tree_gang_lookup_tagged_node_reverse :
148 radix_tree_gang_lookup_tagged_node)(
149 &uobj->uo_pages, off >> PAGE_SHIFT, (void **)ar->ar_pages,
150 maxpages, dense, UVM_PAGE_DIRTY_TAG);
151 } else {
152 npages =
153 (backward ? radix_tree_gang_lookup_node_reverse :
154 radix_tree_gang_lookup_node)(
155 &uobj->uo_pages, off >> PAGE_SHIFT, (void **)ar->ar_pages,
156 maxpages, dense);
157 }
158 if (npages == 0) {
159 uvm_page_array_clear(ar);
160 return ENOENT;
161 }
162 KASSERT(npages <= maxpages);
163 ar->ar_npages = npages;
164 ar->ar_idx = 0;
165 #if defined(DEBUG)
166 for (i = 0; i < ar->ar_npages; i++) {
167 struct vm_page * const pg = ar->ar_pages[i];
168
169 KASSERT(pg != NULL);
170 KASSERT(pg->uobject == uobj);
171 if (backward) {
172 KASSERT(pg->offset <= off);
173 KASSERT(i == 0 ||
174 pg->offset < ar->ar_pages[i - 1]->offset);
175 } else {
176 KASSERT(pg->offset >= off);
177 KASSERT(i == 0 ||
178 pg->offset > ar->ar_pages[i - 1]->offset);
179 }
180 }
181 #endif /* defined(DEBUG) */
182 return 0;
183 }
184
185 /*
186 * uvm_page_array_fill_and_peek:
187 * same as uvm_page_array_peek except that, if the array is empty, try to fill
188 * it first.
189 */
190
191 struct vm_page *
192 uvm_page_array_fill_and_peek(struct uvm_page_array *a, struct uvm_object *uobj,
193 voff_t off, unsigned int nwant, unsigned int flags)
194 {
195 struct vm_page *pg;
196 int error;
197
198 pg = uvm_page_array_peek(a);
199 if (pg != NULL) {
200 return pg;
201 }
202 error = uvm_page_array_fill(a, uobj, off, nwant, flags);
203 if (error != 0) {
204 return NULL;
205 }
206 pg = uvm_page_array_peek(a);
207 KASSERT(pg != NULL);
208 return pg;
209 }
210