Fri Jan 21 19:27:09 2011 UTC ()
Cleanup/add some asserts.  no functional change.


(matt)
diff -r1.53 -r1.54 src/sys/uvm/uvm_pglist.c

cvs diff -r1.53 -r1.54 src/sys/uvm/uvm_pglist.c (switch to unified diff)

--- src/sys/uvm/uvm_pglist.c 2011/01/21 16:56:38 1.53
+++ src/sys/uvm/uvm_pglist.c 2011/01/21 19:27:09 1.54
@@ -1,566 +1,573 @@ @@ -1,566 +1,573 @@
1/* $NetBSD: uvm_pglist.c,v 1.53 2011/01/21 16:56:38 cegger Exp $ */ 1/* $NetBSD: uvm_pglist.c,v 1.54 2011/01/21 19:27:09 matt Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1997 The NetBSD Foundation, Inc. 4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center. 9 * NASA Ames Research Center.
10 * 10 *
11 * Redistribution and use in source and binary forms, with or without 11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions 12 * modification, are permitted provided that the following conditions
13 * are met: 13 * are met:
14 * 1. Redistributions of source code must retain the above copyright 14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer. 15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright 16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the 17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution. 18 * documentation and/or other materials provided with the distribution.
19 * 19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE. 30 * POSSIBILITY OF SUCH DAMAGE.
31 */ 31 */
32 32
33/* 33/*
34 * uvm_pglist.c: pglist functions 34 * uvm_pglist.c: pglist functions
35 */ 35 */
36 36
37#include <sys/cdefs.h> 37#include <sys/cdefs.h>
38__KERNEL_RCSID(0, "$NetBSD: uvm_pglist.c,v 1.53 2011/01/21 16:56:38 cegger Exp $"); 38__KERNEL_RCSID(0, "$NetBSD: uvm_pglist.c,v 1.54 2011/01/21 19:27:09 matt Exp $");
39 39
40#include <sys/param.h> 40#include <sys/param.h>
41#include <sys/systm.h> 41#include <sys/systm.h>
42#include <sys/malloc.h> 42#include <sys/malloc.h>
43#include <sys/proc.h> 43#include <sys/proc.h>
44 44
45#include <uvm/uvm.h> 45#include <uvm/uvm.h>
46#include <uvm/uvm_pdpolicy.h> 46#include <uvm/uvm_pdpolicy.h>
47 47
48#ifdef VM_PAGE_ALLOC_MEMORY_STATS 48#ifdef VM_PAGE_ALLOC_MEMORY_STATS
49#define STAT_INCR(v) (v)++ 49#define STAT_INCR(v) (v)++
50#define STAT_DECR(v) do { \ 50#define STAT_DECR(v) do { \
51 if ((v) == 0) \ 51 if ((v) == 0) \
52 printf("%s:%d -- Already 0!\n", __FILE__, __LINE__); \ 52 printf("%s:%d -- Already 0!\n", __FILE__, __LINE__); \
53 else \ 53 else \
54 (v)--; \ 54 (v)--; \
55 } while (/*CONSTCOND*/ 0) 55 } while (/*CONSTCOND*/ 0)
56u_long uvm_pglistalloc_npages; 56u_long uvm_pglistalloc_npages;
57#else 57#else
58#define STAT_INCR(v) 58#define STAT_INCR(v)
59#define STAT_DECR(v) 59#define STAT_DECR(v)
60#endif 60#endif
61 61
62/* 62/*
63 * uvm_pglistalloc: allocate a list of pages 63 * uvm_pglistalloc: allocate a list of pages
64 * 64 *
65 * => allocated pages are placed onto an rlist. rlist is 65 * => allocated pages are placed onto an rlist. rlist is
66 * initialized by uvm_pglistalloc. 66 * initialized by uvm_pglistalloc.
67 * => returns 0 on success or errno on failure 67 * => returns 0 on success or errno on failure
68 * => implementation allocates a single segment if any constraints are 68 * => implementation allocates a single segment if any constraints are
69 * imposed by call arguments. 69 * imposed by call arguments.
70 * => doesn't take into account clean non-busy pages on inactive list 70 * => doesn't take into account clean non-busy pages on inactive list
71 * that could be used(?) 71 * that could be used(?)
72 * => params: 72 * => params:
73 * size the size of the allocation, rounded to page size. 73 * size the size of the allocation, rounded to page size.
74 * low the low address of the allowed allocation range. 74 * low the low address of the allowed allocation range.
75 * high the high address of the allowed allocation range. 75 * high the high address of the allowed allocation range.
76 * alignment memory must be aligned to this power-of-two boundary. 76 * alignment memory must be aligned to this power-of-two boundary.
77 * boundary no segment in the allocation may cross this 77 * boundary no segment in the allocation may cross this
78 * power-of-two boundary (relative to zero). 78 * power-of-two boundary (relative to zero).
79 */ 79 */
80 80
81static void 81static void
82uvm_pglist_add(struct vm_page *pg, struct pglist *rlist) 82uvm_pglist_add(struct vm_page *pg, struct pglist *rlist)
83{ 83{
84 int free_list, color, pgflidx; 84 int free_list, color, pgflidx;
85 85
86 KASSERT(mutex_owned(&uvm_fpageqlock)); 86 KASSERT(mutex_owned(&uvm_fpageqlock));
87 87
88#if PGFL_NQUEUES != 2 88#if PGFL_NQUEUES != 2
89#error uvm_pglistalloc needs to be updated 89#error uvm_pglistalloc needs to be updated
90#endif 90#endif
91 91
92 free_list = uvm_page_lookup_freelist(pg); 92 free_list = uvm_page_lookup_freelist(pg);
93 color = VM_PGCOLOR_BUCKET(pg); 93 color = VM_PGCOLOR_BUCKET(pg);
94 pgflidx = (pg->flags & PG_ZERO) ? PGFL_ZEROS : PGFL_UNKNOWN; 94 pgflidx = (pg->flags & PG_ZERO) ? PGFL_ZEROS : PGFL_UNKNOWN;
95#ifdef NOT_DEBUG 95#ifdef NOT_DEBUG
96 struct vm_page *tp; 96 struct vm_page *tp;
97 LIST_FOREACH(tp, 97 LIST_FOREACH(tp,
98 &uvm.page_free[free_list].pgfl_buckets[color].pgfl_queues[pgflidx], 98 &uvm.page_free[free_list].pgfl_buckets[color].pgfl_queues[pgflidx],
99 pageq.list) { 99 pageq.list) {
100 if (tp == pg) 100 if (tp == pg)
101 break; 101 break;
102 } 102 }
103 if (tp == NULL) 103 if (tp == NULL)
104 panic("uvm_pglistalloc: page not on freelist"); 104 panic("uvm_pglistalloc: page not on freelist");
105#endif 105#endif
106 LIST_REMOVE(pg, pageq.list); /* global */ 106 LIST_REMOVE(pg, pageq.list); /* global */
107 LIST_REMOVE(pg, listq.list); /* cpu */ 107 LIST_REMOVE(pg, listq.list); /* cpu */
108 uvmexp.free--; 108 uvmexp.free--;
109 if (pg->flags & PG_ZERO) 109 if (pg->flags & PG_ZERO)
110 uvmexp.zeropages--; 110 uvmexp.zeropages--;
111 VM_FREE_PAGE_TO_CPU(pg)->pages[pgflidx]--; 111 VM_FREE_PAGE_TO_CPU(pg)->pages[pgflidx]--;
112 pg->flags = PG_CLEAN; 112 pg->flags = PG_CLEAN;
113 pg->pqflags = 0; 113 pg->pqflags = 0;
114 pg->uobject = NULL; 114 pg->uobject = NULL;
115 pg->uanon = NULL; 115 pg->uanon = NULL;
116 TAILQ_INSERT_TAIL(rlist, pg, pageq.queue); 116 TAILQ_INSERT_TAIL(rlist, pg, pageq.queue);
117 STAT_INCR(uvm_pglistalloc_npages); 117 STAT_INCR(uvm_pglistalloc_npages);
118} 118}
119 119
120static int 120static int
121uvm_pglistalloc_c_ps(struct vm_physseg *ps, int num, paddr_t low, paddr_t high, 121uvm_pglistalloc_c_ps(struct vm_physseg *ps, int num, paddr_t low, paddr_t high,
122 paddr_t alignment, paddr_t boundary, struct pglist *rlist) 122 paddr_t alignment, paddr_t boundary, struct pglist *rlist)
123{ 123{
124 signed int try, limit, tryidx, end, idx, skip; 124 signed int try, limit, tryidx, end, idx, skip;
125 struct vm_page *pgs; 125 struct vm_page *pgs;
126 int pagemask; 126 int pagemask;
127 bool second_pass; 127 bool second_pass;
128#ifdef DEBUG 128#ifdef DEBUG
129 paddr_t idxpa, lastidxpa; 129 paddr_t idxpa, lastidxpa;
130 int cidx = 0; /* XXX: GCC */ 130 int cidx = 0; /* XXX: GCC */
131#endif 131#endif
132#ifdef PGALLOC_VERBOSE 132#ifdef PGALLOC_VERBOSE
133 printf("pgalloc: contig %d pgs from psi %ld\n", num, 133 printf("pgalloc: contig %d pgs from psi %ld\n", num,
134 (long)(ps - vm_physmem)); 134 (long)(ps - vm_physmem));
135#endif 135#endif
136 136
137 KASSERT(mutex_owned(&uvm_fpageqlock)); 137 KASSERT(mutex_owned(&uvm_fpageqlock));
138 138
139 low = atop(low); 139 low = atop(low);
140 high = atop(high); 140 high = atop(high);
141 alignment = atop(alignment); 141 alignment = atop(alignment);
142 142
143 /* 143 /*
144 * We start our search at the just after where the last allocation 144 * We start our search at the just after where the last allocation
145 * succeeded. 145 * succeeded.
146 */ 146 */
147 try = roundup2(max(low, ps->avail_start + ps->start_hint), alignment); 147 try = roundup2(max(low, ps->avail_start + ps->start_hint), alignment);
148 limit = min(high, ps->avail_end); 148 limit = min(high, ps->avail_end);
149 pagemask = ~((boundary >> PAGE_SHIFT) - 1); 149 pagemask = ~((boundary >> PAGE_SHIFT) - 1);
150 skip = 0; 150 skip = 0;
151 second_pass = false; 151 second_pass = false;
152 pgs = ps->pgs; 152 pgs = ps->pgs;
153 153
154 for (;;) { 154 for (;;) {
155 bool ok = true; 155 bool ok = true;
156 signed int cnt; 156 signed int cnt;
157 157
158 if (try + num > limit) { 158 if (try + num > limit) {
159 if (ps->start_hint == 0 || second_pass) { 159 if (ps->start_hint == 0 || second_pass) {
160 /* 160 /*
161 * We've run past the allowable range. 161 * We've run past the allowable range.
162 */ 162 */
163 return 0; /* FAIL = 0 pages*/ 163 return 0; /* FAIL = 0 pages*/
164 } 164 }
165 /* 165 /*
166 * We've wrapped around the end of this segment 166 * We've wrapped around the end of this segment
167 * so restart at the beginning but now our limit 167 * so restart at the beginning but now our limit
168 * is were we started. 168 * is were we started.
169 */ 169 */
170 second_pass = true; 170 second_pass = true;
171 try = roundup2(max(low, ps->avail_start), alignment); 171 try = roundup2(max(low, ps->avail_start), alignment);
172 limit = min(high, ps->avail_start + ps->start_hint); 172 limit = min(high, ps->avail_start + ps->start_hint);
173 skip = 0; 173 skip = 0;
174 continue; 174 continue;
175 } 175 }
176 if (boundary != 0 && 176 if (boundary != 0 &&
177 ((try ^ (try + num - 1)) & pagemask) != 0) { 177 ((try ^ (try + num - 1)) & pagemask) != 0) {
178 /* 178 /*
179 * Region crosses boundary. Jump to the boundary 179 * Region crosses boundary. Jump to the boundary
180 * just crossed and ensure alignment. 180 * just crossed and ensure alignment.
181 */ 181 */
182 try = (try + num - 1) & pagemask; 182 try = (try + num - 1) & pagemask;
183 try = roundup2(try, alignment); 183 try = roundup2(try, alignment);
184 skip = 0; 184 skip = 0;
185 continue; 185 continue;
186 } 186 }
187#ifdef DEBUG 187#ifdef DEBUG
188 /* 188 /*
189 * Make sure this is a managed physical page. 189 * Make sure this is a managed physical page.
190 */ 190 */
191 191
192 if (vm_physseg_find(try, &cidx) != ps - vm_physmem) 192 if (vm_physseg_find(try, &cidx) != ps - vm_physmem)
193 panic("pgalloc contig: botch1"); 193 panic("pgalloc contig: botch1");
194 if (cidx != try - ps->start) 194 if (cidx != try - ps->start)
195 panic("pgalloc contig: botch2"); 195 panic("pgalloc contig: botch2");
196 if (vm_physseg_find(try + num - 1, &cidx) != ps - vm_physmem) 196 if (vm_physseg_find(try + num - 1, &cidx) != ps - vm_physmem)
197 panic("pgalloc contig: botch3"); 197 panic("pgalloc contig: botch3");
198 if (cidx != try - ps->start + num - 1) 198 if (cidx != try - ps->start + num - 1)
199 panic("pgalloc contig: botch4"); 199 panic("pgalloc contig: botch4");
200#endif 200#endif
201 tryidx = try - ps->start; 201 tryidx = try - ps->start;
202 end = tryidx + num; 202 end = tryidx + num;
203 203
204 /* 204 /*
205 * Found a suitable starting page. See if the range is free. 205 * Found a suitable starting page. See if the range is free.
206 */ 206 */
207#ifdef PGALLOC_VERBOSE 207#ifdef PGALLOC_VERBOSE
208 printf("%s: ps=%p try=%#x end=%#x skip=%#x, align=0x%"PRIxPADDR, 208 printf("%s: ps=%p try=%#x end=%#x skip=%#x, align=%#"PRIxPADDR,
209 __func__, ps, tryidx, end, skip, alignment); 209 __func__, ps, tryidx, end, skip, alignment);
210#endif 210#endif
211 /* 211 /*
212 * We start at the end and work backwards since if we find a 212 * We start at the end and work backwards since if we find a
213 * non-free page, it makes no sense to continue. 213 * non-free page, it makes no sense to continue.
214 * 214 *
215 * But on the plus size we have "vetted" some number of free 215 * But on the plus size we have "vetted" some number of free
216 * pages. If this iteration fails, we may be able to skip 216 * pages. If this iteration fails, we may be able to skip
217 * testing most of those pages again in the next pass. 217 * testing most of those pages again in the next pass.
218 */ 218 */
219 for (idx = end - 1; idx >= tryidx + skip; idx--) { 219 for (idx = end - 1; idx >= tryidx + skip; idx--) {
220 if (VM_PAGE_IS_FREE(&pgs[idx]) == 0) { 220 if (VM_PAGE_IS_FREE(&pgs[idx]) == 0) {
221 ok = false; 221 ok = false;
222 break; 222 break;
223 } 223 }
224 224
225#ifdef DEBUG 225#ifdef DEBUG
226 if (idx > tryidx) { 226 if (idx > tryidx) {
227 idxpa = VM_PAGE_TO_PHYS(&pgs[idx]); 227 idxpa = VM_PAGE_TO_PHYS(&pgs[idx]);
228 lastidxpa = VM_PAGE_TO_PHYS(&pgs[idx - 1]); 228 lastidxpa = VM_PAGE_TO_PHYS(&pgs[idx - 1]);
229 if ((lastidxpa + PAGE_SIZE) != idxpa) { 229 if ((lastidxpa + PAGE_SIZE) != idxpa) {
230 /* 230 /*
231 * Region not contiguous. 231 * Region not contiguous.
232 */ 232 */
233 panic("pgalloc contig: botch5"); 233 panic("pgalloc contig: botch5");
234 } 234 }
235 if (boundary != 0 && 235 if (boundary != 0 &&
236 ((lastidxpa ^ idxpa) & ~(boundary - 1)) 236 ((lastidxpa ^ idxpa) & ~(boundary - 1))
237 != 0) { 237 != 0) {
238 /* 238 /*
239 * Region crosses boundary. 239 * Region crosses boundary.
240 */ 240 */
241 panic("pgalloc contig: botch6"); 241 panic("pgalloc contig: botch6");
242 } 242 }
243 } 243 }
244#endif 244#endif
245 } 245 }
246 246
247 if (ok) { 247 if (ok) {
248 while (skip-- > 0) { 248 while (skip-- > 0) {
249 KDASSERT(VM_PAGE_IS_FREE(&pgs[tryidx + skip])); 249 KDASSERT(VM_PAGE_IS_FREE(&pgs[tryidx + skip]));
250 } 250 }
251#ifdef PGALLOC_VERBOSE 251#ifdef PGALLOC_VERBOSE
252 printf(": ok\n"); 252 printf(": ok\n");
253#endif 253#endif
254 break; 254 break;
255 } 255 }
256 256
257#ifdef PGALLOC_VERBOSE 257#ifdef PGALLOC_VERBOSE
258 printf(": non-free at %#x\n", idx - tryidx); 258 printf(": non-free at %#x\n", idx - tryidx);
259#endif 259#endif
260 /* 260 /*
261 * count the number of pages we can advance 261 * count the number of pages we can advance
262 * since we know they aren't all free. 262 * since we know they aren't all free.
263 */ 263 */
264 cnt = idx + 1 - tryidx; 264 cnt = idx + 1 - tryidx;
265 /* 265 /*
266 * now round up that to the needed alignment. 266 * now round up that to the needed alignment.
267 */ 267 */
268 cnt = roundup2(cnt, alignment); 268 cnt = roundup2(cnt, alignment);
269 /* 269 /*
270 * The number of pages we can skip checking  270 * The number of pages we can skip checking
271 * (might be 0 if cnt > num). 271 * (might be 0 if cnt > num).
272 */ 272 */
273 skip = max(num - cnt, 0); 273 skip = max(num - cnt, 0);
274 try += cnt; 274 try += cnt;
275 } 275 }
276 276
277 /* 277 /*
278 * we have a chunk of memory that conforms to the requested constraints. 278 * we have a chunk of memory that conforms to the requested constraints.
279 */ 279 */
280 for (idx = tryidx, pgs += idx; idx < end; idx++, pgs++) 280 for (idx = tryidx, pgs += idx; idx < end; idx++, pgs++)
281 uvm_pglist_add(pgs, rlist); 281 uvm_pglist_add(pgs, rlist);
282 282
283 /* 283 /*
284 * the next time we need to search this segment, start after this 284 * the next time we need to search this segment, start after this
285 * chunk of pages we just allocated. 285 * chunk of pages we just allocated.
286 */ 286 */
287 ps->start_hint = tryidx + num; 287 ps->start_hint = tryidx + num;
288 288
289#ifdef PGALLOC_VERBOSE 289#ifdef PGALLOC_VERBOSE
290 printf("got %d pgs\n", num); 290 printf("got %d pgs\n", num);
291#endif 291#endif
292 return num; /* number of pages allocated */ 292 return num; /* number of pages allocated */
293} 293}
294 294
295static int 295static int
296uvm_pglistalloc_contig(int num, paddr_t low, paddr_t high, paddr_t alignment, 296uvm_pglistalloc_contig(int num, paddr_t low, paddr_t high, paddr_t alignment,
297 paddr_t boundary, struct pglist *rlist) 297 paddr_t boundary, struct pglist *rlist)
298{ 298{
299 int fl, psi; 299 int fl, psi;
300 struct vm_physseg *ps; 300 struct vm_physseg *ps;
301 int error; 301 int error;
302 302
303 /* Default to "lose". */ 303 /* Default to "lose". */
304 error = ENOMEM; 304 error = ENOMEM;
305 305
306 /* 306 /*
307 * Block all memory allocation and lock the free list. 307 * Block all memory allocation and lock the free list.
308 */ 308 */
309 mutex_spin_enter(&uvm_fpageqlock); 309 mutex_spin_enter(&uvm_fpageqlock);
310 310
311 /* Are there even any free pages? */ 311 /* Are there even any free pages? */
312 if (uvmexp.free <= (uvmexp.reserve_pagedaemon + uvmexp.reserve_kernel)) 312 if (uvmexp.free <= (uvmexp.reserve_pagedaemon + uvmexp.reserve_kernel))
313 goto out; 313 goto out;
314 314
315 for (fl = 0; fl < VM_NFREELIST; fl++) { 315 for (fl = 0; fl < VM_NFREELIST; fl++) {
316#if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST) 316#if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
317 for (psi = vm_nphysseg - 1 ; psi >= 0 ; psi--) 317 for (psi = vm_nphysseg - 1 ; psi >= 0 ; psi--)
318#else 318#else
319 for (psi = 0 ; psi < vm_nphysseg ; psi++) 319 for (psi = 0 ; psi < vm_nphysseg ; psi++)
320#endif 320#endif
321 { 321 {
322 ps = &vm_physmem[psi]; 322 ps = &vm_physmem[psi];
323 323
324 if (ps->free_list != fl) 324 if (ps->free_list != fl)
325 continue; 325 continue;
326 326
327 num -= uvm_pglistalloc_c_ps(ps, num, low, high, 327 num -= uvm_pglistalloc_c_ps(ps, num, low, high,
328 alignment, boundary, rlist); 328 alignment, boundary, rlist);
329 if (num == 0) { 329 if (num == 0) {
330#ifdef PGALLOC_VERBOSE 330#ifdef PGALLOC_VERBOSE
331 printf("pgalloc: %"PRIxMAX"-%"PRIxMAX"\n", 331 printf("pgalloc: %"PRIxMAX"-%"PRIxMAX"\n",
332 (uintmax_t) VM_PAGE_TO_PHYS(TAILQ_FIRST(rlist)), 332 (uintmax_t) VM_PAGE_TO_PHYS(TAILQ_FIRST(rlist)),
333 (uintmax_t) VM_PAGE_TO_PHYS(TAILQ_LAST(rlist, pglist))); 333 (uintmax_t) VM_PAGE_TO_PHYS(TAILQ_LAST(rlist, pglist)));
334#endif 334#endif
335 error = 0; 335 error = 0;
336 goto out; 336 goto out;
337 } 337 }
338 } 338 }
339 } 339 }
340 340
341out: 341out:
342 /* 342 /*
343 * check to see if we need to generate some free pages waking 343 * check to see if we need to generate some free pages waking
344 * the pagedaemon. 344 * the pagedaemon.
345 */ 345 */
346 346
347 uvm_kick_pdaemon(); 347 uvm_kick_pdaemon();
348 mutex_spin_exit(&uvm_fpageqlock); 348 mutex_spin_exit(&uvm_fpageqlock);
349 return (error); 349 return (error);
350} 350}
351 351
352static int 352static int
353uvm_pglistalloc_s_ps(struct vm_physseg *ps, int num, paddr_t low, paddr_t high, 353uvm_pglistalloc_s_ps(struct vm_physseg *ps, int num, paddr_t low, paddr_t high,
354 struct pglist *rlist) 354 struct pglist *rlist)
355{ 355{
356 int todo, limit, try; 356 int todo, limit, try;
357 struct vm_page *pg; 357 struct vm_page *pg;
358 bool second_pass; 358 bool second_pass;
359#ifdef DEBUG 
360 int cidx = 0; /* XXX: GCC */ 
361#endif 
362#ifdef PGALLOC_VERBOSE 359#ifdef PGALLOC_VERBOSE
363 printf("pgalloc: simple %d pgs from psi %ld\n", num, 360 printf("pgalloc: simple %d pgs from psi %ld\n", num,
364 (long)(ps - vm_physmem)); 361 (long)(ps - vm_physmem));
365#endif 362#endif
366 363
367 KASSERT(mutex_owned(&uvm_fpageqlock)); 364 KASSERT(mutex_owned(&uvm_fpageqlock));
 365 KASSERT(ps->start <= ps->avail_start);
 366 KASSERT(ps->start <= ps->avail_end);
 367 KASSERT(ps->avail_start <= ps->end);
 368 KASSERT(ps->avail_end <= ps->end);
368 369
369 low = atop(low); 370 low = atop(low);
370 high = atop(high); 371 high = atop(high);
371 todo = num; 372 todo = num;
372 try = max(low, ps->avail_start + ps->start_hint); 373 try = max(low, ps->avail_start + ps->start_hint);
373 limit = min(high, ps->avail_end); 374 limit = min(high, ps->avail_end);
374 pg = &ps->pgs[try - ps->start]; 375 pg = &ps->pgs[try - ps->start];
375 second_pass = false; 376 second_pass = false;
376 377
377 for (;; try++, pg++) { 378 for (;; try++, pg++) {
378 if (try >= limit) { 379 if (try >= limit) {
379 if (ps->start_hint == 0 || second_pass) 380 if (ps->start_hint == 0 || second_pass)
380 break; 381 break;
381 second_pass = true; 382 second_pass = true;
382 try = max(low, ps->avail_start); 383 try = max(low, ps->avail_start);
383 limit = min(high, ps->avail_start + ps->start_hint); 384 limit = min(high, ps->avail_start + ps->start_hint);
384 pg = &ps->pgs[try - ps->start]; 385 pg = &ps->pgs[try - ps->start];
385 continue; 386 continue;
386 } 387 }
387#ifdef DEBUG 388#ifdef DEBUG
388 if (vm_physseg_find(try, &cidx) != ps - vm_physmem) 389 {
389 panic("pgalloc simple: botch1"); 390 int cidx = 0;
390 if (cidx != (try - ps->start)) 391 const int bank = vm_physseg_find(try, &cidx);
391 panic("pgalloc simple: botch2"); 392 KASSERTMSG(bank == ps - vm_physmem,
 393 ("vm_physseg_find(%#x) (%d) != ps %zd",
 394 try, bank, ps - vm_physmem));
 395 KASSERTMSG(cidx == try - ps->start,
 396 ("vm_physseg_find(%#x): %#x != off %"PRIxPADDR,
 397 try, cidx, try - ps->start));
 398 }
392#endif 399#endif
393 if (VM_PAGE_IS_FREE(pg) == 0) 400 if (VM_PAGE_IS_FREE(pg) == 0)
394 continue; 401 continue;
395 402
396 uvm_pglist_add(pg, rlist); 403 uvm_pglist_add(pg, rlist);
397 if (--todo == 0) { 404 if (--todo == 0) {
398 break; 405 break;
399 } 406 }
400 } 407 }
401 408
402 /* 409 /*
403 * The next time we need to search this segment, 410 * The next time we need to search this segment,
404 * start just after the pages we just allocated. 411 * start just after the pages we just allocated.
405 */ 412 */
406 ps->start_hint = try + 1 - ps->start; 413 ps->start_hint = try + 1 - ps->start;
407 414
408#ifdef PGALLOC_VERBOSE 415#ifdef PGALLOC_VERBOSE
409 printf("got %d pgs\n", num - todo); 416 printf("got %d pgs\n", num - todo);
410#endif 417#endif
411 return (num - todo); /* number of pages allocated */ 418 return (num - todo); /* number of pages allocated */
412} 419}
413 420
414static int 421static int
415uvm_pglistalloc_simple(int num, paddr_t low, paddr_t high, 422uvm_pglistalloc_simple(int num, paddr_t low, paddr_t high,
416 struct pglist *rlist, int waitok) 423 struct pglist *rlist, int waitok)
417{ 424{
418 int fl, psi, error; 425 int fl, psi, error;
419 struct vm_physseg *ps; 426 struct vm_physseg *ps;
420 427
421 /* Default to "lose". */ 428 /* Default to "lose". */
422 error = ENOMEM; 429 error = ENOMEM;
423 430
424again: 431again:
425 /* 432 /*
426 * Block all memory allocation and lock the free list. 433 * Block all memory allocation and lock the free list.
427 */ 434 */
428 mutex_spin_enter(&uvm_fpageqlock); 435 mutex_spin_enter(&uvm_fpageqlock);
429 436
430 /* Are there even any free pages? */ 437 /* Are there even any free pages? */
431 if (uvmexp.free <= (uvmexp.reserve_pagedaemon + uvmexp.reserve_kernel)) 438 if (uvmexp.free <= (uvmexp.reserve_pagedaemon + uvmexp.reserve_kernel))
432 goto out; 439 goto out;
433 440
434 for (fl = 0; fl < VM_NFREELIST; fl++) { 441 for (fl = 0; fl < VM_NFREELIST; fl++) {
435#if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST) 442#if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
436 for (psi = vm_nphysseg - 1 ; psi >= 0 ; psi--) 443 for (psi = vm_nphysseg - 1 ; psi >= 0 ; psi--)
437#else 444#else
438 for (psi = 0 ; psi < vm_nphysseg ; psi++) 445 for (psi = 0 ; psi < vm_nphysseg ; psi++)
439#endif 446#endif
440 { 447 {
441 ps = &vm_physmem[psi]; 448 ps = &vm_physmem[psi];
442 449
443 if (ps->free_list != fl) 450 if (ps->free_list != fl)
444 continue; 451 continue;
445 452
446 num -= uvm_pglistalloc_s_ps(ps, num, low, high, rlist); 453 num -= uvm_pglistalloc_s_ps(ps, num, low, high, rlist);
447 if (num == 0) { 454 if (num == 0) {
448 error = 0; 455 error = 0;
449 goto out; 456 goto out;
450 } 457 }
451 } 458 }
452 459
453 } 460 }
454 461
455out: 462out:
456 /* 463 /*
457 * check to see if we need to generate some free pages waking 464 * check to see if we need to generate some free pages waking
458 * the pagedaemon. 465 * the pagedaemon.
459 */ 466 */
460 467
461 uvm_kick_pdaemon(); 468 uvm_kick_pdaemon();
462 mutex_spin_exit(&uvm_fpageqlock); 469 mutex_spin_exit(&uvm_fpageqlock);
463 470
464 if (error) { 471 if (error) {
465 if (waitok) { 472 if (waitok) {
466 /* XXX perhaps some time limitation? */ 473 /* XXX perhaps some time limitation? */
467#ifdef DEBUG 474#ifdef DEBUG
468 printf("pglistalloc waiting\n"); 475 printf("pglistalloc waiting\n");
469#endif 476#endif
470 uvm_wait("pglalloc"); 477 uvm_wait("pglalloc");
471 goto again; 478 goto again;
472 } else 479 } else
473 uvm_pglistfree(rlist); 480 uvm_pglistfree(rlist);
474 } 481 }
475#ifdef PGALLOC_VERBOSE 482#ifdef PGALLOC_VERBOSE
476 if (!error) 483 if (!error)
477 printf("pgalloc: %"PRIxMAX"..%"PRIxMAX"\n", 484 printf("pgalloc: %"PRIxMAX"..%"PRIxMAX"\n",
478 (uintmax_t) VM_PAGE_TO_PHYS(TAILQ_FIRST(rlist)), 485 (uintmax_t) VM_PAGE_TO_PHYS(TAILQ_FIRST(rlist)),
479 (uintmax_t) VM_PAGE_TO_PHYS(TAILQ_LAST(rlist, pglist))); 486 (uintmax_t) VM_PAGE_TO_PHYS(TAILQ_LAST(rlist, pglist)));
480#endif 487#endif
481 return (error); 488 return (error);
482} 489}
483 490
484int 491int
485uvm_pglistalloc(psize_t size, paddr_t low, paddr_t high, paddr_t alignment, 492uvm_pglistalloc(psize_t size, paddr_t low, paddr_t high, paddr_t alignment,
486 paddr_t boundary, struct pglist *rlist, int nsegs, int waitok) 493 paddr_t boundary, struct pglist *rlist, int nsegs, int waitok)
487{ 494{
488 int num, res; 495 int num, res;
489 496
490 KASSERT((alignment & (alignment - 1)) == 0); 497 KASSERT((alignment & (alignment - 1)) == 0);
491 KASSERT((boundary & (boundary - 1)) == 0); 498 KASSERT((boundary & (boundary - 1)) == 0);
492 499
493 /* 500 /*
494 * Our allocations are always page granularity, so our alignment 501 * Our allocations are always page granularity, so our alignment
495 * must be, too. 502 * must be, too.
496 */ 503 */
497 if (alignment < PAGE_SIZE) 504 if (alignment < PAGE_SIZE)
498 alignment = PAGE_SIZE; 505 alignment = PAGE_SIZE;
499 if (boundary != 0 && boundary < size) 506 if (boundary != 0 && boundary < size)
500 return (EINVAL); 507 return (EINVAL);
501 num = atop(round_page(size)); 508 num = atop(round_page(size));
502 low = roundup2(low, alignment); 509 low = roundup2(low, alignment);
503 510
504 TAILQ_INIT(rlist); 511 TAILQ_INIT(rlist);
505 512
506 if ((nsegs < size >> PAGE_SHIFT) || (alignment != PAGE_SIZE) || 513 if ((nsegs < size >> PAGE_SHIFT) || (alignment != PAGE_SIZE) ||
507 (boundary != 0)) 514 (boundary != 0))
508 res = uvm_pglistalloc_contig(num, low, high, alignment, 515 res = uvm_pglistalloc_contig(num, low, high, alignment,
509 boundary, rlist); 516 boundary, rlist);
510 else 517 else
511 res = uvm_pglistalloc_simple(num, low, high, rlist, waitok); 518 res = uvm_pglistalloc_simple(num, low, high, rlist, waitok);
512 519
513 return (res); 520 return (res);
514} 521}
515 522
516/* 523/*
517 * uvm_pglistfree: free a list of pages 524 * uvm_pglistfree: free a list of pages
518 * 525 *
519 * => pages should already be unmapped 526 * => pages should already be unmapped
520 */ 527 */
521 528
522void 529void
523uvm_pglistfree(struct pglist *list) 530uvm_pglistfree(struct pglist *list)
524{ 531{
525 struct uvm_cpu *ucpu; 532 struct uvm_cpu *ucpu;
526 struct vm_page *pg; 533 struct vm_page *pg;
527 int index, color, queue; 534 int index, color, queue;
528 bool iszero; 535 bool iszero;
529 536
530 /* 537 /*
531 * Lock the free list and free each page. 538 * Lock the free list and free each page.
532 */ 539 */
533 540
534 mutex_spin_enter(&uvm_fpageqlock); 541 mutex_spin_enter(&uvm_fpageqlock);
535 ucpu = curcpu()->ci_data.cpu_uvm; 542 ucpu = curcpu()->ci_data.cpu_uvm;
536 while ((pg = TAILQ_FIRST(list)) != NULL) { 543 while ((pg = TAILQ_FIRST(list)) != NULL) {
537 KASSERT(!uvmpdpol_pageisqueued_p(pg)); 544 KASSERT(!uvmpdpol_pageisqueued_p(pg));
538 TAILQ_REMOVE(list, pg, pageq.queue); 545 TAILQ_REMOVE(list, pg, pageq.queue);
539 iszero = (pg->flags & PG_ZERO); 546 iszero = (pg->flags & PG_ZERO);
540 pg->pqflags = PQ_FREE; 547 pg->pqflags = PQ_FREE;
541#ifdef DEBUG 548#ifdef DEBUG
542 pg->uobject = (void *)0xdeadbeef; 549 pg->uobject = (void *)0xdeadbeef;
543 pg->uanon = (void *)0xdeadbeef; 550 pg->uanon = (void *)0xdeadbeef;
544#endif /* DEBUG */ 551#endif /* DEBUG */
545#ifdef DEBUG 552#ifdef DEBUG
546 if (iszero) 553 if (iszero)
547 uvm_pagezerocheck(pg); 554 uvm_pagezerocheck(pg);
548#endif /* DEBUG */ 555#endif /* DEBUG */
549 index = uvm_page_lookup_freelist(pg); 556 index = uvm_page_lookup_freelist(pg);
550 color = VM_PGCOLOR_BUCKET(pg); 557 color = VM_PGCOLOR_BUCKET(pg);
551 queue = iszero ? PGFL_ZEROS : PGFL_UNKNOWN; 558 queue = iszero ? PGFL_ZEROS : PGFL_UNKNOWN;
552 pg->offset = (uintptr_t)ucpu; 559 pg->offset = (uintptr_t)ucpu;
553 LIST_INSERT_HEAD(&uvm.page_free[index].pgfl_buckets[color]. 560 LIST_INSERT_HEAD(&uvm.page_free[index].pgfl_buckets[color].
554 pgfl_queues[queue], pg, pageq.list); 561 pgfl_queues[queue], pg, pageq.list);
555 LIST_INSERT_HEAD(&ucpu->page_free[index].pgfl_buckets[color]. 562 LIST_INSERT_HEAD(&ucpu->page_free[index].pgfl_buckets[color].
556 pgfl_queues[queue], pg, listq.list); 563 pgfl_queues[queue], pg, listq.list);
557 uvmexp.free++; 564 uvmexp.free++;
558 if (iszero) 565 if (iszero)
559 uvmexp.zeropages++; 566 uvmexp.zeropages++;
560 ucpu->pages[queue]++; 567 ucpu->pages[queue]++;
561 STAT_DECR(uvm_pglistalloc_npages); 568 STAT_DECR(uvm_pglistalloc_npages);
562 } 569 }
563 if (ucpu->pages[PGFL_ZEROS] < ucpu->pages[PGFL_UNKNOWN]) 570 if (ucpu->pages[PGFL_ZEROS] < ucpu->pages[PGFL_UNKNOWN])
564 ucpu->page_idle_zero = vm_page_zero_enable; 571 ucpu->page_idle_zero = vm_page_zero_enable;
565 mutex_spin_exit(&uvm_fpageqlock); 572 mutex_spin_exit(&uvm_fpageqlock);
566} 573}