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