uvm_pglist.c revision 1.9 1 /* $NetBSD: uvm_pglist.c,v 1.9 2000/04/24 17:12:01 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * uvm_pglist.c: pglist functions
42 *
43 * XXX: was part of uvm_page but has an incompatable copyright so it
44 * gets its own file now.
45 */
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/malloc.h>
50 #include <sys/proc.h>
51
52 #include <vm/vm.h>
53
54 #include <uvm/uvm.h>
55
56 #ifdef VM_PAGE_ALLOC_MEMORY_STATS
57 #define STAT_INCR(v) (v)++
58 #define STAT_DECR(v) do { \
59 if ((v) == 0) \
60 printf("%s:%d -- Already 0!\n", __FILE__, __LINE__); \
61 else \
62 (v)--; \
63 } while (0)
64 u_long uvm_pglistalloc_npages;
65 #else
66 #define STAT_INCR(v)
67 #define STAT_DECR(v)
68 #endif
69
70 /*
71 * uvm_pglistalloc: allocate a list of pages
72 *
73 * => allocated pages are placed at the tail of rlist. rlist is
74 * assumed to be properly initialized by caller.
75 * => returns 0 on success or errno on failure
76 * => XXX: implementation allocates only a single segment, also
77 * might be able to better advantage of vm_physeg[].
78 * => doesn't take into account clean non-busy pages on inactive list
79 * that could be used(?)
80 * => params:
81 * size the size of the allocation, rounded to page size.
82 * low the low address of the allowed allocation range.
83 * high the high address of the allowed allocation range.
84 * alignment memory must be aligned to this power-of-two boundary.
85 * boundary no segment in the allocation may cross this
86 * power-of-two boundary (relative to zero).
87 */
88
89 int
90 uvm_pglistalloc(size, low, high, alignment, boundary, rlist, nsegs, waitok)
91 psize_t size;
92 paddr_t low, high, alignment, boundary;
93 struct pglist *rlist;
94 int nsegs, waitok;
95 {
96 paddr_t try, idxpa, lastidxpa;
97 int psi;
98 struct vm_page *pgs;
99 int s, tryidx, idx, pgflidx, end, error, free_list;
100 vm_page_t m;
101 u_long pagemask;
102 #ifdef DEBUG
103 vm_page_t tp;
104 #endif
105
106 #ifdef DIAGNOSTIC
107 if ((alignment & (alignment - 1)) != 0)
108 panic("uvm_pglistalloc: alignment must be power of 2");
109
110 if ((boundary & (boundary - 1)) != 0)
111 panic("uvm_pglistalloc: boundary must be power of 2");
112 #endif
113
114 /*
115 * Our allocations are always page granularity, so our alignment
116 * must be, too.
117 */
118 if (alignment < PAGE_SIZE)
119 alignment = PAGE_SIZE;
120
121 size = round_page(size);
122 try = roundup(low, alignment);
123
124 if (boundary != 0 && boundary < size)
125 return (EINVAL);
126
127 pagemask = ~(boundary - 1);
128
129 /* Default to "lose". */
130 error = ENOMEM;
131
132 /*
133 * Block all memory allocation and lock the free list.
134 */
135 s = uvm_lock_fpageq(); /* lock free page queue */
136
137 /* Are there even any free pages? */
138 if (uvmexp.free <= (uvmexp.reserve_pagedaemon +
139 uvmexp.reserve_kernel))
140 goto out;
141
142 for (;; try += alignment) {
143 if (try + size > high) {
144 /*
145 * We've run past the allowable range.
146 */
147 goto out;
148 }
149
150 /*
151 * Make sure this is a managed physical page.
152 */
153
154 if ((psi = vm_physseg_find(atop(try), &idx)) == -1)
155 continue; /* managed? */
156 if (vm_physseg_find(atop(try + size), NULL) != psi)
157 continue; /* end must be in this segment */
158
159 tryidx = idx;
160 end = idx + (size / PAGE_SIZE);
161 pgs = vm_physmem[psi].pgs;
162
163 /*
164 * Found a suitable starting page. See of the range is free.
165 */
166 for (; idx < end; idx++) {
167 if (VM_PAGE_IS_FREE(&pgs[idx]) == 0) {
168 /*
169 * Page not available.
170 */
171 break;
172 }
173
174 idxpa = VM_PAGE_TO_PHYS(&pgs[idx]);
175
176 if (idx > tryidx) {
177 lastidxpa = VM_PAGE_TO_PHYS(&pgs[idx - 1]);
178
179 if ((lastidxpa + PAGE_SIZE) != idxpa) {
180 /*
181 * Region not contiguous.
182 */
183 break;
184 }
185 if (boundary != 0 &&
186 ((lastidxpa ^ idxpa) & pagemask) != 0) {
187 /*
188 * Region crosses boundary.
189 */
190 break;
191 }
192 }
193 }
194
195 if (idx == end) {
196 /*
197 * Woo hoo! Found one.
198 */
199 break;
200 }
201 }
202
203 #if PGFL_NQUEUES != 2
204 #error uvm_pglistalloc needs to be updated
205 #endif
206
207 /*
208 * we have a chunk of memory that conforms to the requested constraints.
209 */
210 idx = tryidx;
211 while (idx < end) {
212 m = &pgs[idx];
213 free_list = uvm_page_lookup_freelist(m);
214 pgflidx = (m->flags & PG_ZERO) ? PGFL_ZEROS : PGFL_UNKNOWN;
215 #ifdef DEBUG
216 for (tp = TAILQ_FIRST(&uvm.page_free[
217 free_list].pgfl_queues[pgflidx]);
218 tp != NULL;
219 tp = TAILQ_NEXT(tp, pageq)) {
220 if (tp == m)
221 break;
222 }
223 if (tp == NULL)
224 panic("uvm_pglistalloc: page not on freelist");
225 #endif
226 TAILQ_REMOVE(&uvm.page_free[free_list].pgfl_queues[pgflidx],
227 m, pageq);
228 uvmexp.free--;
229 if (m->flags & PG_ZERO)
230 uvmexp.zeropages--;
231 m->flags = PG_CLEAN;
232 m->pqflags = 0;
233 m->uobject = NULL;
234 m->uanon = NULL;
235 m->wire_count = 0;
236 m->loan_count = 0;
237 TAILQ_INSERT_TAIL(rlist, m, pageq);
238 idx++;
239 STAT_INCR(uvm_pglistalloc_npages);
240 }
241 error = 0;
242
243 out:
244 uvm_unlock_fpageq(s);
245
246 /*
247 * check to see if we need to generate some free pages waking
248 * the pagedaemon.
249 * XXX: we read uvm.free without locking
250 */
251
252 if (uvmexp.free < uvmexp.freemin ||
253 (uvmexp.free < uvmexp.freetarg &&
254 uvmexp.inactive < uvmexp.inactarg))
255 wakeup(&uvm.pagedaemon);
256
257 return (error);
258 }
259
260 /*
261 * uvm_pglistfree: free a list of pages
262 *
263 * => pages should already be unmapped
264 */
265
266 void
267 uvm_pglistfree(list)
268 struct pglist *list;
269 {
270 vm_page_t m;
271 int s;
272
273 /*
274 * Block all memory allocation and lock the free list.
275 */
276 s = uvm_lock_fpageq();
277
278 while ((m = list->tqh_first) != NULL) {
279 #ifdef DIAGNOSTIC
280 if (m->pqflags & (PQ_ACTIVE|PQ_INACTIVE))
281 panic("uvm_pglistfree: active/inactive page!");
282 #endif
283 TAILQ_REMOVE(list, m, pageq);
284 m->pqflags = PQ_FREE;
285 TAILQ_INSERT_TAIL(&uvm.page_free[
286 uvm_page_lookup_freelist(m)].pgfl_queues[PGFL_UNKNOWN],
287 m, pageq);
288 uvmexp.free++;
289 if (uvmexp.zeropages < UVM_PAGEZERO_TARGET)
290 uvm.page_idle_zero = vm_page_zero_enable;
291 STAT_DECR(uvm_pglistalloc_npages);
292 }
293
294 uvm_unlock_fpageq(s);
295 }
296