uvm_pglist.c revision 1.21 1 /* $NetBSD: uvm_pglist.c,v 1.21 2002/06/02 14:44:46 drochner 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
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: uvm_pglist.c,v 1.21 2002/06/02 14:44:46 drochner Exp $");
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/malloc.h>
50 #include <sys/proc.h>
51
52 #include <uvm/uvm.h>
53
54 #ifdef VM_PAGE_ALLOC_MEMORY_STATS
55 #define STAT_INCR(v) (v)++
56 #define STAT_DECR(v) do { \
57 if ((v) == 0) \
58 printf("%s:%d -- Already 0!\n", __FILE__, __LINE__); \
59 else \
60 (v)--; \
61 } while (0)
62 u_long uvm_pglistalloc_npages;
63 #else
64 #define STAT_INCR(v)
65 #define STAT_DECR(v)
66 #endif
67
68 /*
69 * uvm_pglistalloc: allocate a list of pages
70 *
71 * => allocated pages are placed at the tail of rlist. rlist is
72 * assumed to be properly initialized by caller.
73 * => returns 0 on success or errno on failure
74 * => XXX: implementation allocates only a single segment, also
75 * might be able to better advantage of vm_physeg[].
76 * => doesn't take into account clean non-busy pages on inactive list
77 * that could be used(?)
78 * => params:
79 * size the size of the allocation, rounded to page size.
80 * low the low address of the allowed allocation range.
81 * high the high address of the allowed allocation range.
82 * alignment memory must be aligned to this power-of-two boundary.
83 * boundary no segment in the allocation may cross this
84 * power-of-two boundary (relative to zero).
85 */
86
87 static void uvm_pglist_add(struct vm_page *, struct pglist *);
88 static int uvm_pglistalloc_contig(psize_t, paddr_t, paddr_t, paddr_t, paddr_t,
89 struct pglist *);
90 static int uvm_pglistalloc_simple(psize_t, paddr_t, paddr_t,
91 struct pglist *, int);
92
93 static void
94 uvm_pglist_add(pg, rlist)
95 struct vm_page *pg;
96 struct pglist *rlist;
97 {
98 int free_list, color, pgflidx;
99 #ifdef DEBUG
100 struct vm_page *tp;
101 #endif
102
103 #if PGFL_NQUEUES != 2
104 #error uvm_pglistalloc needs to be updated
105 #endif
106
107 free_list = uvm_page_lookup_freelist(pg);
108 color = VM_PGCOLOR_BUCKET(pg);
109 pgflidx = (pg->flags & PG_ZERO) ? PGFL_ZEROS : PGFL_UNKNOWN;
110 #ifdef DEBUG
111 for (tp = TAILQ_FIRST(&uvm.page_free[
112 free_list].pgfl_buckets[color].pgfl_queues[pgflidx]);
113 tp != NULL;
114 tp = TAILQ_NEXT(tp, pageq)) {
115 if (tp == pg)
116 break;
117 }
118 if (tp == NULL)
119 panic("uvm_pglistalloc: page not on freelist");
120 #endif
121 TAILQ_REMOVE(&uvm.page_free[free_list].pgfl_buckets[
122 color].pgfl_queues[pgflidx], pg, pageq);
123 uvmexp.free--;
124 if (pg->flags & PG_ZERO)
125 uvmexp.zeropages--;
126 pg->flags = PG_CLEAN;
127 pg->pqflags = 0;
128 pg->uobject = NULL;
129 pg->uanon = NULL;
130 TAILQ_INSERT_TAIL(rlist, pg, pageq);
131 STAT_INCR(uvm_pglistalloc_npages);
132 }
133
134 static int
135 uvm_pglistalloc_contig(size, low, high, alignment, boundary, rlist)
136 psize_t size;
137 paddr_t low, high, alignment, boundary;
138 struct pglist *rlist;
139 {
140 paddr_t try, idxpa, lastidxpa;
141 int psi;
142 struct vm_page *pgs;
143 int s, tryidx, idx, end, error;
144 u_long pagemask;
145
146 if (boundary != 0 && boundary < size)
147 return (EINVAL);
148 pagemask = ~(boundary - 1);
149
150 /* Default to "lose". */
151 error = ENOMEM;
152
153 /*
154 * Block all memory allocation and lock the free list.
155 */
156
157 s = uvm_lock_fpageq();
158
159 /* Are there even any free pages? */
160 if (uvmexp.free <= (uvmexp.reserve_pagedaemon + uvmexp.reserve_kernel))
161 goto out;
162
163 for (try = low;; try += alignment) {
164 if (try + size > high) {
165
166 /*
167 * We've run past the allowable range.
168 */
169
170 goto out;
171 }
172
173 /*
174 * Make sure this is a managed physical page.
175 */
176
177 if ((psi = vm_physseg_find(atop(try), &idx)) == -1)
178 continue; /* managed? */
179 if (vm_physseg_find(atop(try + size), NULL) != psi)
180 continue; /* end must be in this segment */
181 tryidx = idx;
182 end = idx + (size / PAGE_SIZE);
183 pgs = vm_physmem[psi].pgs;
184
185 /*
186 * Found a suitable starting page. See of the range is free.
187 */
188
189 for (; idx < end; idx++) {
190 if (VM_PAGE_IS_FREE(&pgs[idx]) == 0) {
191 break;
192 }
193 idxpa = VM_PAGE_TO_PHYS(&pgs[idx]);
194 if (idx > tryidx) {
195 lastidxpa = VM_PAGE_TO_PHYS(&pgs[idx - 1]);
196 if ((lastidxpa + PAGE_SIZE) != idxpa) {
197
198 /*
199 * Region not contiguous.
200 */
201
202 break;
203 }
204 if (boundary != 0 &&
205 ((lastidxpa ^ idxpa) & pagemask) != 0) {
206
207 /*
208 * Region crosses boundary.
209 */
210
211 break;
212 }
213 }
214 }
215 if (idx == end) {
216 break;
217 }
218 }
219
220 /*
221 * we have a chunk of memory that conforms to the requested constraints.
222 */
223 idx = tryidx;
224 while (idx < end) {
225 uvm_pglist_add(&pgs[idx++], rlist);
226 }
227 error = 0;
228
229 out:
230 /*
231 * check to see if we need to generate some free pages waking
232 * the pagedaemon.
233 */
234
235 UVM_KICK_PDAEMON();
236 uvm_unlock_fpageq(s);
237 return (error);
238 }
239
240 static int
241 uvm_pglistalloc_simple(size, low, high, rlist, waitok)
242 psize_t size;
243 paddr_t low, high;
244 struct pglist *rlist;
245 int waitok;
246 {
247 psize_t try;
248 int psi;
249 struct vm_page *pg;
250 int s, todo, idx, error;
251
252 /* Default to "lose". */
253 error = ENOMEM;
254
255 todo = size / PAGE_SIZE;
256
257 again:
258 /*
259 * Block all memory allocation and lock the free list.
260 */
261
262 s = uvm_lock_fpageq();
263
264 /* Are there even any free pages? */
265 if (uvmexp.free <= (uvmexp.reserve_pagedaemon + uvmexp.reserve_kernel))
266 goto out;
267
268 for (try = low; try < high; try += PAGE_SIZE) {
269
270 /*
271 * Make sure this is a managed physical page.
272 */
273
274 if ((psi = vm_physseg_find(atop(try), &idx)) == -1)
275 continue; /* managed? */
276 pg = &vm_physmem[psi].pgs[idx];
277 if (VM_PAGE_IS_FREE(pg) == 0)
278 continue;
279
280 uvm_pglist_add(pg, rlist);
281 if (--todo == 0) {
282 error = 0;
283 goto out;
284 }
285 }
286
287 out:
288 /*
289 * check to see if we need to generate some free pages waking
290 * the pagedaemon.
291 */
292
293 UVM_KICK_PDAEMON();
294 uvm_unlock_fpageq(s);
295 if (error) {
296 if (waitok) {
297 /* XXX perhaps some time limitation? */
298 #ifdef DEBUG
299 printf("pglistalloc waiting\n");
300 #endif
301 uvm_wait("pglalloc");
302 goto again;
303 } else
304 uvm_pglistfree(rlist);
305 }
306 return (error);
307 }
308
309 int
310 uvm_pglistalloc(size, low, high, alignment, boundary, rlist, nsegs, waitok)
311 psize_t size;
312 paddr_t low, high, alignment, boundary;
313 struct pglist *rlist;
314 int nsegs, waitok;
315 {
316 int res;
317
318 KASSERT((alignment & (alignment - 1)) == 0);
319 KASSERT((boundary & (boundary - 1)) == 0);
320
321 /*
322 * Our allocations are always page granularity, so our alignment
323 * must be, too.
324 */
325 if (alignment < PAGE_SIZE)
326 alignment = PAGE_SIZE;
327 size = round_page(size);
328 low = roundup(low, alignment);
329
330 TAILQ_INIT(rlist);
331
332 if ((nsegs < size / PAGE_SIZE) || (alignment != PAGE_SIZE)
333 || (boundary != 0))
334 res = uvm_pglistalloc_contig(size, low, high, alignment,
335 boundary, rlist);
336 else
337 res = uvm_pglistalloc_simple(size, low, high, rlist, waitok);
338
339 return (res);
340 }
341
342 /*
343 * uvm_pglistfree: free a list of pages
344 *
345 * => pages should already be unmapped
346 */
347
348 void
349 uvm_pglistfree(list)
350 struct pglist *list;
351 {
352 struct vm_page *pg;
353 int s;
354
355 /*
356 * Lock the free list and free each page.
357 */
358
359 s = uvm_lock_fpageq();
360 while ((pg = TAILQ_FIRST(list)) != NULL) {
361 KASSERT((pg->pqflags & (PQ_ACTIVE|PQ_INACTIVE)) == 0);
362 TAILQ_REMOVE(list, pg, pageq);
363 pg->pqflags = PQ_FREE;
364 TAILQ_INSERT_TAIL(&uvm.page_free[uvm_page_lookup_freelist(pg)].
365 pgfl_buckets[VM_PGCOLOR_BUCKET(pg)].
366 pgfl_queues[PGFL_UNKNOWN], pg, pageq);
367 uvmexp.free++;
368 if (uvmexp.zeropages < UVM_PAGEZERO_TARGET)
369 uvm.page_idle_zero = vm_page_zero_enable;
370 STAT_DECR(uvm_pglistalloc_npages);
371 }
372 uvm_unlock_fpageq(s);
373 }
374