uvm_pglist.c revision 1.12 1 /* $NetBSD: uvm_pglist.c,v 1.12 2000/11/25 06:28:00 chs 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/param.h>
45 #include <sys/systm.h>
46 #include <sys/malloc.h>
47 #include <sys/proc.h>
48
49 #include <uvm/uvm.h>
50
51 #ifdef VM_PAGE_ALLOC_MEMORY_STATS
52 #define STAT_INCR(v) (v)++
53 #define STAT_DECR(v) do { \
54 if ((v) == 0) \
55 printf("%s:%d -- Already 0!\n", __FILE__, __LINE__); \
56 else \
57 (v)--; \
58 } while (0)
59 u_long uvm_pglistalloc_npages;
60 #else
61 #define STAT_INCR(v)
62 #define STAT_DECR(v)
63 #endif
64
65 /*
66 * uvm_pglistalloc: allocate a list of pages
67 *
68 * => allocated pages are placed at the tail of rlist. rlist is
69 * assumed to be properly initialized by caller.
70 * => returns 0 on success or errno on failure
71 * => XXX: implementation allocates only a single segment, also
72 * might be able to better advantage of vm_physeg[].
73 * => doesn't take into account clean non-busy pages on inactive list
74 * that could be used(?)
75 * => params:
76 * size the size of the allocation, rounded to page size.
77 * low the low address of the allowed allocation range.
78 * high the high address of the allowed allocation range.
79 * alignment memory must be aligned to this power-of-two boundary.
80 * boundary no segment in the allocation may cross this
81 * power-of-two boundary (relative to zero).
82 */
83
84 int
85 uvm_pglistalloc(size, low, high, alignment, boundary, rlist, nsegs, waitok)
86 psize_t size;
87 paddr_t low, high, alignment, boundary;
88 struct pglist *rlist;
89 int nsegs, waitok;
90 {
91 paddr_t try, idxpa, lastidxpa;
92 int psi;
93 struct vm_page *pgs;
94 int s, tryidx, idx, pgflidx, end, error, free_list;
95 vm_page_t m;
96 u_long pagemask;
97 #ifdef DEBUG
98 vm_page_t tp;
99 #endif
100
101 #ifdef DIAGNOSTIC
102 if ((alignment & (alignment - 1)) != 0)
103 panic("uvm_pglistalloc: alignment must be power of 2");
104
105 if ((boundary & (boundary - 1)) != 0)
106 panic("uvm_pglistalloc: boundary must be power of 2");
107 #endif
108
109 /*
110 * Our allocations are always page granularity, so our alignment
111 * must be, too.
112 */
113 if (alignment < PAGE_SIZE)
114 alignment = PAGE_SIZE;
115
116 size = round_page(size);
117 try = roundup(low, alignment);
118
119 if (boundary != 0 && boundary < size)
120 return (EINVAL);
121
122 pagemask = ~(boundary - 1);
123
124 /* Default to "lose". */
125 error = ENOMEM;
126
127 /*
128 * Block all memory allocation and lock the free list.
129 */
130 s = uvm_lock_fpageq();
131
132 /* Are there even any free pages? */
133 if (uvmexp.free <= (uvmexp.reserve_pagedaemon + uvmexp.reserve_kernel))
134 goto out;
135
136 for (;; try += alignment) {
137 if (try + size > high) {
138
139 /*
140 * We've run past the allowable range.
141 */
142
143 goto out;
144 }
145
146 /*
147 * Make sure this is a managed physical page.
148 */
149
150 if ((psi = vm_physseg_find(atop(try), &idx)) == -1)
151 continue; /* managed? */
152 if (vm_physseg_find(atop(try + size), NULL) != psi)
153 continue; /* end must be in this segment */
154
155 tryidx = idx;
156 end = idx + (size / PAGE_SIZE);
157 pgs = vm_physmem[psi].pgs;
158
159 /*
160 * Found a suitable starting page. See of the range is free.
161 */
162
163 for (; idx < end; idx++) {
164 if (VM_PAGE_IS_FREE(&pgs[idx]) == 0) {
165 break;
166 }
167 idxpa = VM_PAGE_TO_PHYS(&pgs[idx]);
168 if (idx > tryidx) {
169 lastidxpa = VM_PAGE_TO_PHYS(&pgs[idx - 1]);
170 if ((lastidxpa + PAGE_SIZE) != idxpa) {
171
172 /*
173 * Region not contiguous.
174 */
175
176 break;
177 }
178 if (boundary != 0 &&
179 ((lastidxpa ^ idxpa) & pagemask) != 0) {
180
181 /*
182 * Region crosses boundary.
183 */
184
185 break;
186 }
187 }
188 }
189 if (idx == end) {
190 break;
191 }
192 }
193
194 #if PGFL_NQUEUES != 2
195 #error uvm_pglistalloc needs to be updated
196 #endif
197
198 /*
199 * we have a chunk of memory that conforms to the requested constraints.
200 */
201 idx = tryidx;
202 while (idx < end) {
203 m = &pgs[idx];
204 free_list = uvm_page_lookup_freelist(m);
205 pgflidx = (m->flags & PG_ZERO) ? PGFL_ZEROS : PGFL_UNKNOWN;
206 #ifdef DEBUG
207 for (tp = TAILQ_FIRST(&uvm.page_free[
208 free_list].pgfl_queues[pgflidx]);
209 tp != NULL;
210 tp = TAILQ_NEXT(tp, pageq)) {
211 if (tp == m)
212 break;
213 }
214 if (tp == NULL)
215 panic("uvm_pglistalloc: page not on freelist");
216 #endif
217 TAILQ_REMOVE(&uvm.page_free[free_list].pgfl_queues[pgflidx],
218 m, pageq);
219 uvmexp.free--;
220 if (m->flags & PG_ZERO)
221 uvmexp.zeropages--;
222 m->flags = PG_CLEAN;
223 m->pqflags = 0;
224 m->uobject = NULL;
225 m->uanon = NULL;
226 m->version++;
227 TAILQ_INSERT_TAIL(rlist, m, pageq);
228 idx++;
229 STAT_INCR(uvm_pglistalloc_npages);
230 }
231 error = 0;
232
233 out:
234 /*
235 * check to see if we need to generate some free pages waking
236 * the pagedaemon.
237 */
238
239 if (uvmexp.free + uvmexp.paging < uvmexp.freemin ||
240 (uvmexp.free + uvmexp.paging < uvmexp.freetarg &&
241 uvmexp.inactive < uvmexp.inactarg)) {
242 wakeup(&uvm.pagedaemon);
243 }
244
245 uvm_unlock_fpageq(s);
246
247 return (error);
248 }
249
250 /*
251 * uvm_pglistfree: free a list of pages
252 *
253 * => pages should already be unmapped
254 */
255
256 void
257 uvm_pglistfree(list)
258 struct pglist *list;
259 {
260 vm_page_t m;
261 int s;
262
263 /*
264 * Block all memory allocation and lock the free list.
265 */
266 s = uvm_lock_fpageq();
267
268 while ((m = list->tqh_first) != NULL) {
269 #ifdef DIAGNOSTIC
270 if (m->pqflags & (PQ_ACTIVE|PQ_INACTIVE))
271 panic("uvm_pglistfree: active/inactive page!");
272 #endif
273 TAILQ_REMOVE(list, m, pageq);
274 m->pqflags = PQ_FREE;
275 TAILQ_INSERT_TAIL(&uvm.page_free[
276 uvm_page_lookup_freelist(m)].pgfl_queues[PGFL_UNKNOWN],
277 m, pageq);
278 uvmexp.free++;
279 if (uvmexp.zeropages < UVM_PAGEZERO_TARGET)
280 uvm.page_idle_zero = vm_page_zero_enable;
281 STAT_DECR(uvm_pglistalloc_npages);
282 }
283
284 uvm_unlock_fpageq(s);
285 }
286