uvm_pglist.c revision 1.83 1 1.83 ad /* $NetBSD: uvm_pglist.c,v 1.83 2020/06/11 19:20:47 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.83 ad __KERNEL_RCSID(0, "$NetBSD: uvm_pglist.c,v 1.83 2020/06/11 19:20:47 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.20 drochner if (pg->flags & PG_ZERO)
102 1.74 ad CPU_COUNT(CPU_COUNT_ZEROPAGES, -1);
103 1.20 drochner pg->flags = PG_CLEAN;
104 1.20 drochner pg->uobject = NULL;
105 1.20 drochner pg->uanon = NULL;
106 1.42 ad TAILQ_INSERT_TAIL(rlist, pg, pageq.queue);
107 1.20 drochner STAT_INCR(uvm_pglistalloc_npages);
108 1.20 drochner }
109 1.20 drochner
110 1.20 drochner static int
111 1.68 cherry uvm_pglistalloc_c_ps(uvm_physseg_t psi, int num, paddr_t low, paddr_t high,
112 1.33 thorpej paddr_t alignment, paddr_t boundary, struct pglist *rlist)
113 1.1 mrg {
114 1.66 matt signed int candidate, limit, candidateidx, end, idx, skip;
115 1.24 drochner int pagemask;
116 1.52 matt bool second_pass;
117 1.24 drochner #ifdef DEBUG
118 1.22 drochner paddr_t idxpa, lastidxpa;
119 1.68 cherry paddr_t cidx = 0; /* XXX: GCC */
120 1.22 drochner #endif
121 1.24 drochner #ifdef PGALLOC_VERBOSE
122 1.80 rin printf("pgalloc: contig %d pgs from psi %d\n", num, psi);
123 1.24 drochner #endif
124 1.1 mrg
125 1.52 matt low = atop(low);
126 1.52 matt high = atop(high);
127 1.52 matt alignment = atop(alignment);
128 1.52 matt
129 1.52 matt /*
130 1.57 matt * Make sure that physseg falls within with range to be allocated from.
131 1.57 matt */
132 1.68 cherry if (high <= uvm_physseg_get_avail_start(psi) || low >= uvm_physseg_get_avail_end(psi))
133 1.57 matt return 0;
134 1.57 matt
135 1.57 matt /*
136 1.52 matt * We start our search at the just after where the last allocation
137 1.52 matt * succeeded.
138 1.52 matt */
139 1.71 riastrad candidate = roundup2(uimax(low, uvm_physseg_get_avail_start(psi) +
140 1.68 cherry uvm_physseg_get_start_hint(psi)), alignment);
141 1.71 riastrad limit = uimin(high, uvm_physseg_get_avail_end(psi));
142 1.24 drochner pagemask = ~((boundary >> PAGE_SHIFT) - 1);
143 1.52 matt skip = 0;
144 1.52 matt second_pass = false;
145 1.12 chs
146 1.24 drochner for (;;) {
147 1.52 matt bool ok = true;
148 1.52 matt signed int cnt;
149 1.52 matt
150 1.66 matt if (candidate + num > limit) {
151 1.68 cherry if (uvm_physseg_get_start_hint(psi) == 0 || second_pass) {
152 1.52 matt /*
153 1.52 matt * We've run past the allowable range.
154 1.52 matt */
155 1.52 matt return 0; /* FAIL = 0 pages*/
156 1.52 matt }
157 1.3 mrg /*
158 1.52 matt * We've wrapped around the end of this segment
159 1.52 matt * so restart at the beginning but now our limit
160 1.52 matt * is were we started.
161 1.3 mrg */
162 1.52 matt second_pass = true;
163 1.71 riastrad candidate = roundup2(uimax(low, uvm_physseg_get_avail_start(psi)), alignment);
164 1.71 riastrad limit = uimin(limit, uvm_physseg_get_avail_start(psi) +
165 1.68 cherry uvm_physseg_get_start_hint(psi));
166 1.52 matt skip = 0;
167 1.52 matt continue;
168 1.3 mrg }
169 1.24 drochner if (boundary != 0 &&
170 1.66 matt ((candidate ^ (candidate + num - 1)) & pagemask) != 0) {
171 1.24 drochner /*
172 1.24 drochner * Region crosses boundary. Jump to the boundary
173 1.24 drochner * just crossed and ensure alignment.
174 1.24 drochner */
175 1.66 matt candidate = (candidate + num - 1) & pagemask;
176 1.66 matt candidate = roundup2(candidate, alignment);
177 1.52 matt skip = 0;
178 1.24 drochner continue;
179 1.24 drochner }
180 1.22 drochner #ifdef DEBUG
181 1.3 mrg /*
182 1.3 mrg * Make sure this is a managed physical page.
183 1.3 mrg */
184 1.3 mrg
185 1.68 cherry if (uvm_physseg_find(candidate, &cidx) != psi)
186 1.22 drochner panic("pgalloc contig: botch1");
187 1.68 cherry if (cidx != candidate - uvm_physseg_get_start(psi))
188 1.22 drochner panic("pgalloc contig: botch2");
189 1.68 cherry if (uvm_physseg_find(candidate + num - 1, &cidx) != psi)
190 1.22 drochner panic("pgalloc contig: botch3");
191 1.68 cherry if (cidx != candidate - uvm_physseg_get_start(psi) + num - 1)
192 1.31 junyoung panic("pgalloc contig: botch4");
193 1.22 drochner #endif
194 1.68 cherry candidateidx = candidate - uvm_physseg_get_start(psi);
195 1.66 matt end = candidateidx + num;
196 1.3 mrg
197 1.3 mrg /*
198 1.24 drochner * Found a suitable starting page. See if the range is free.
199 1.3 mrg */
200 1.52 matt #ifdef PGALLOC_VERBOSE
201 1.80 rin printf("%s: psi=%d candidate=%#x end=%#x skip=%#x, align=%#"PRIxPADDR,
202 1.80 rin __func__, psi, candidateidx, end, skip, alignment);
203 1.52 matt #endif
204 1.52 matt /*
205 1.52 matt * We start at the end and work backwards since if we find a
206 1.52 matt * non-free page, it makes no sense to continue.
207 1.52 matt *
208 1.52 matt * But on the plus size we have "vetted" some number of free
209 1.52 matt * pages. If this iteration fails, we may be able to skip
210 1.52 matt * testing most of those pages again in the next pass.
211 1.52 matt */
212 1.66 matt for (idx = end - 1; idx >= candidateidx + skip; idx--) {
213 1.68 cherry if (VM_PAGE_IS_FREE(uvm_physseg_get_pg(psi, idx)) == 0) {
214 1.52 matt ok = false;
215 1.3 mrg break;
216 1.52 matt }
217 1.24 drochner
218 1.24 drochner #ifdef DEBUG
219 1.66 matt if (idx > candidateidx) {
220 1.68 cherry idxpa = VM_PAGE_TO_PHYS(uvm_physseg_get_pg(psi, idx));
221 1.68 cherry lastidxpa = VM_PAGE_TO_PHYS(uvm_physseg_get_pg(psi, idx - 1));
222 1.12 chs if ((lastidxpa + PAGE_SIZE) != idxpa) {
223 1.3 mrg /*
224 1.3 mrg * Region not contiguous.
225 1.3 mrg */
226 1.22 drochner panic("pgalloc contig: botch5");
227 1.3 mrg }
228 1.3 mrg if (boundary != 0 &&
229 1.24 drochner ((lastidxpa ^ idxpa) & ~(boundary - 1))
230 1.24 drochner != 0) {
231 1.3 mrg /*
232 1.3 mrg * Region crosses boundary.
233 1.3 mrg */
234 1.24 drochner panic("pgalloc contig: botch6");
235 1.3 mrg }
236 1.3 mrg }
237 1.24 drochner #endif
238 1.3 mrg }
239 1.52 matt
240 1.52 matt if (ok) {
241 1.52 matt while (skip-- > 0) {
242 1.68 cherry KDASSERT(VM_PAGE_IS_FREE(uvm_physseg_get_pg(psi, candidateidx + skip)));
243 1.52 matt }
244 1.52 matt #ifdef PGALLOC_VERBOSE
245 1.52 matt printf(": ok\n");
246 1.52 matt #endif
247 1.3 mrg break;
248 1.52 matt }
249 1.24 drochner
250 1.52 matt #ifdef PGALLOC_VERBOSE
251 1.66 matt printf(": non-free at %#x\n", idx - candidateidx);
252 1.52 matt #endif
253 1.52 matt /*
254 1.52 matt * count the number of pages we can advance
255 1.52 matt * since we know they aren't all free.
256 1.52 matt */
257 1.66 matt cnt = idx + 1 - candidateidx;
258 1.52 matt /*
259 1.52 matt * now round up that to the needed alignment.
260 1.52 matt */
261 1.52 matt cnt = roundup2(cnt, alignment);
262 1.52 matt /*
263 1.52 matt * The number of pages we can skip checking
264 1.52 matt * (might be 0 if cnt > num).
265 1.52 matt */
266 1.71 riastrad skip = uimax(num - cnt, 0);
267 1.66 matt candidate += cnt;
268 1.1 mrg }
269 1.1 mrg
270 1.3 mrg /*
271 1.3 mrg * we have a chunk of memory that conforms to the requested constraints.
272 1.3 mrg */
273 1.68 cherry for (idx = candidateidx; idx < end; idx++)
274 1.68 cherry uvm_pglist_add(uvm_physseg_get_pg(psi, idx), rlist);
275 1.52 matt
276 1.52 matt /*
277 1.52 matt * the next time we need to search this segment, start after this
278 1.52 matt * chunk of pages we just allocated.
279 1.52 matt */
280 1.68 cherry uvm_physseg_set_start_hint(psi, candidate + num -
281 1.68 cherry uvm_physseg_get_avail_start(psi));
282 1.68 cherry KASSERTMSG(uvm_physseg_get_start_hint(psi) <=
283 1.68 cherry uvm_physseg_get_avail_end(psi) - uvm_physseg_get_avail_start(psi),
284 1.62 jym "%x %u (%#x) <= %#"PRIxPADDR" - %#"PRIxPADDR" (%#"PRIxPADDR")",
285 1.66 matt candidate + num,
286 1.68 cherry uvm_physseg_get_start_hint(psi), uvm_physseg_get_start_hint(psi),
287 1.68 cherry uvm_physseg_get_avail_end(psi), uvm_physseg_get_avail_start(psi),
288 1.68 cherry uvm_physseg_get_avail_end(psi) - uvm_physseg_get_avail_start(psi));
289 1.24 drochner
290 1.24 drochner #ifdef PGALLOC_VERBOSE
291 1.24 drochner printf("got %d pgs\n", num);
292 1.24 drochner #endif
293 1.52 matt return num; /* number of pages allocated */
294 1.22 drochner }
295 1.22 drochner
296 1.22 drochner static int
297 1.33 thorpej uvm_pglistalloc_contig(int num, paddr_t low, paddr_t high, paddr_t alignment,
298 1.33 thorpej paddr_t boundary, struct pglist *rlist)
299 1.22 drochner {
300 1.68 cherry int fl;
301 1.38 ad int error;
302 1.22 drochner
303 1.68 cherry uvm_physseg_t psi;
304 1.22 drochner /* Default to "lose". */
305 1.22 drochner error = ENOMEM;
306 1.22 drochner
307 1.22 drochner /*
308 1.22 drochner * Block all memory allocation and lock the free list.
309 1.22 drochner */
310 1.78 ad uvm_pgfl_lock();
311 1.22 drochner
312 1.22 drochner /* Are there even any free pages? */
313 1.83 ad if (uvm_availmem(false) <=
314 1.79 ad (uvmexp.reserve_pagedaemon + uvmexp.reserve_kernel))
315 1.22 drochner goto out;
316 1.22 drochner
317 1.22 drochner for (fl = 0; fl < VM_NFREELIST; fl++) {
318 1.22 drochner #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
319 1.68 cherry for (psi = uvm_physseg_get_last(); uvm_physseg_valid_p(psi); psi = uvm_physseg_get_prev(psi))
320 1.22 drochner #else
321 1.68 cherry for (psi = uvm_physseg_get_first(); uvm_physseg_valid_p(psi); psi = uvm_physseg_get_next(psi))
322 1.22 drochner #endif
323 1.22 drochner {
324 1.68 cherry if (uvm_physseg_get_free_list(psi) != fl)
325 1.22 drochner continue;
326 1.22 drochner
327 1.68 cherry num -= uvm_pglistalloc_c_ps(psi, num, low, high,
328 1.24 drochner alignment, boundary, rlist);
329 1.24 drochner if (num == 0) {
330 1.24 drochner #ifdef PGALLOC_VERBOSE
331 1.44 reinoud printf("pgalloc: %"PRIxMAX"-%"PRIxMAX"\n",
332 1.44 reinoud (uintmax_t) VM_PAGE_TO_PHYS(TAILQ_FIRST(rlist)),
333 1.44 reinoud (uintmax_t) VM_PAGE_TO_PHYS(TAILQ_LAST(rlist, pglist)));
334 1.22 drochner #endif
335 1.22 drochner error = 0;
336 1.22 drochner goto out;
337 1.22 drochner }
338 1.22 drochner }
339 1.22 drochner }
340 1.20 drochner
341 1.20 drochner out:
342 1.20 drochner /*
343 1.20 drochner * check to see if we need to generate some free pages waking
344 1.20 drochner * the pagedaemon.
345 1.20 drochner */
346 1.20 drochner
347 1.78 ad uvm_pgfl_unlock();
348 1.36 yamt uvm_kick_pdaemon();
349 1.20 drochner return (error);
350 1.20 drochner }
351 1.20 drochner
352 1.24 drochner static int
353 1.68 cherry uvm_pglistalloc_s_ps(uvm_physseg_t psi, int num, paddr_t low, paddr_t high,
354 1.33 thorpej struct pglist *rlist)
355 1.22 drochner {
356 1.66 matt int todo, limit, candidate;
357 1.22 drochner struct vm_page *pg;
358 1.52 matt bool second_pass;
359 1.24 drochner #ifdef PGALLOC_VERBOSE
360 1.68 cherry printf("pgalloc: simple %d pgs from psi %zd\n", num, psi);
361 1.24 drochner #endif
362 1.22 drochner
363 1.68 cherry KASSERT(uvm_physseg_get_start(psi) <= uvm_physseg_get_avail_start(psi));
364 1.68 cherry KASSERT(uvm_physseg_get_start(psi) <= uvm_physseg_get_avail_end(psi));
365 1.68 cherry KASSERT(uvm_physseg_get_avail_start(psi) <= uvm_physseg_get_end(psi));
366 1.68 cherry KASSERT(uvm_physseg_get_avail_end(psi) <= uvm_physseg_get_end(psi));
367 1.39 ad
368 1.52 matt low = atop(low);
369 1.52 matt high = atop(high);
370 1.24 drochner todo = num;
371 1.71 riastrad candidate = uimax(low, uvm_physseg_get_avail_start(psi) +
372 1.68 cherry uvm_physseg_get_start_hint(psi));
373 1.71 riastrad limit = uimin(high, uvm_physseg_get_avail_end(psi));
374 1.68 cherry pg = uvm_physseg_get_pg(psi, candidate - uvm_physseg_get_start(psi));
375 1.52 matt second_pass = false;
376 1.52 matt
377 1.57 matt /*
378 1.57 matt * Make sure that physseg falls within with range to be allocated from.
379 1.57 matt */
380 1.68 cherry if (high <= uvm_physseg_get_avail_start(psi) ||
381 1.68 cherry low >= uvm_physseg_get_avail_end(psi))
382 1.57 matt return 0;
383 1.57 matt
384 1.60 enami again:
385 1.66 matt for (;; candidate++, pg++) {
386 1.66 matt if (candidate >= limit) {
387 1.68 cherry if (uvm_physseg_get_start_hint(psi) == 0 || second_pass) {
388 1.66 matt candidate = limit - 1;
389 1.52 matt break;
390 1.57 matt }
391 1.52 matt second_pass = true;
392 1.71 riastrad candidate = uimax(low, uvm_physseg_get_avail_start(psi));
393 1.71 riastrad limit = uimin(limit, uvm_physseg_get_avail_start(psi) +
394 1.68 cherry uvm_physseg_get_start_hint(psi));
395 1.68 cherry pg = uvm_physseg_get_pg(psi, candidate - uvm_physseg_get_start(psi));
396 1.60 enami goto again;
397 1.52 matt }
398 1.58 matt #if defined(DEBUG)
399 1.54 matt {
400 1.68 cherry paddr_t cidx = 0;
401 1.68 cherry const uvm_physseg_t bank = uvm_physseg_find(candidate, &cidx);
402 1.68 cherry KDASSERTMSG(bank == psi,
403 1.70 skrll "uvm_physseg_find(%#x) (%"PRIxPHYSSEG ") != psi %"PRIxPHYSSEG,
404 1.68 cherry candidate, bank, psi);
405 1.68 cherry KDASSERTMSG(cidx == candidate - uvm_physseg_get_start(psi),
406 1.68 cherry "uvm_physseg_find(%#x): %#"PRIxPADDR" != off %"PRIxPADDR,
407 1.68 cherry candidate, cidx, candidate - uvm_physseg_get_start(psi));
408 1.54 matt }
409 1.22 drochner #endif
410 1.22 drochner if (VM_PAGE_IS_FREE(pg) == 0)
411 1.22 drochner continue;
412 1.22 drochner
413 1.22 drochner uvm_pglist_add(pg, rlist);
414 1.52 matt if (--todo == 0) {
415 1.22 drochner break;
416 1.52 matt }
417 1.22 drochner }
418 1.24 drochner
419 1.52 matt /*
420 1.52 matt * The next time we need to search this segment,
421 1.52 matt * start just after the pages we just allocated.
422 1.52 matt */
423 1.68 cherry uvm_physseg_set_start_hint(psi, candidate + 1 - uvm_physseg_get_avail_start(psi));
424 1.68 cherry KASSERTMSG(uvm_physseg_get_start_hint(psi) <= uvm_physseg_get_avail_end(psi) -
425 1.68 cherry uvm_physseg_get_avail_start(psi),
426 1.62 jym "%#x %u (%#x) <= %#"PRIxPADDR" - %#"PRIxPADDR" (%#"PRIxPADDR")",
427 1.66 matt candidate + 1,
428 1.68 cherry uvm_physseg_get_start_hint(psi),
429 1.68 cherry uvm_physseg_get_start_hint(psi),
430 1.68 cherry uvm_physseg_get_avail_end(psi),
431 1.68 cherry uvm_physseg_get_avail_start(psi),
432 1.68 cherry uvm_physseg_get_avail_end(psi) - uvm_physseg_get_avail_start(psi));
433 1.52 matt
434 1.24 drochner #ifdef PGALLOC_VERBOSE
435 1.24 drochner printf("got %d pgs\n", num - todo);
436 1.24 drochner #endif
437 1.24 drochner return (num - todo); /* number of pages allocated */
438 1.22 drochner }
439 1.22 drochner
440 1.20 drochner static int
441 1.33 thorpej uvm_pglistalloc_simple(int num, paddr_t low, paddr_t high,
442 1.33 thorpej struct pglist *rlist, int waitok)
443 1.20 drochner {
444 1.68 cherry int fl, error;
445 1.68 cherry uvm_physseg_t psi;
446 1.72 mrg int count = 0;
447 1.20 drochner
448 1.20 drochner /* Default to "lose". */
449 1.20 drochner error = ENOMEM;
450 1.20 drochner
451 1.20 drochner again:
452 1.20 drochner /*
453 1.20 drochner * Block all memory allocation and lock the free list.
454 1.20 drochner */
455 1.78 ad uvm_pgfl_lock();
456 1.72 mrg count++;
457 1.20 drochner
458 1.20 drochner /* Are there even any free pages? */
459 1.83 ad if (uvm_availmem(false) <=
460 1.79 ad (uvmexp.reserve_pagedaemon + uvmexp.reserve_kernel))
461 1.20 drochner goto out;
462 1.20 drochner
463 1.22 drochner for (fl = 0; fl < VM_NFREELIST; fl++) {
464 1.22 drochner #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
465 1.68 cherry for (psi = uvm_physseg_get_last(); uvm_physseg_valid_p(psi); psi = uvm_physseg_get_prev(psi))
466 1.22 drochner #else
467 1.68 cherry for (psi = uvm_physseg_get_first(); uvm_physseg_valid_p(psi); psi = uvm_physseg_get_next(psi))
468 1.22 drochner #endif
469 1.22 drochner {
470 1.68 cherry if (uvm_physseg_get_free_list(psi) != fl)
471 1.22 drochner continue;
472 1.22 drochner
473 1.68 cherry num -= uvm_pglistalloc_s_ps(psi, num, low, high, rlist);
474 1.24 drochner if (num == 0) {
475 1.22 drochner error = 0;
476 1.22 drochner goto out;
477 1.22 drochner }
478 1.22 drochner }
479 1.20 drochner
480 1.3 mrg }
481 1.1 mrg
482 1.1 mrg out:
483 1.3 mrg /*
484 1.3 mrg * check to see if we need to generate some free pages waking
485 1.3 mrg * the pagedaemon.
486 1.3 mrg */
487 1.15 chs
488 1.78 ad uvm_pgfl_unlock();
489 1.36 yamt uvm_kick_pdaemon();
490 1.38 ad
491 1.20 drochner if (error) {
492 1.20 drochner if (waitok) {
493 1.20 drochner /* XXX perhaps some time limitation? */
494 1.20 drochner #ifdef DEBUG
495 1.72 mrg if (count == 1)
496 1.72 mrg printf("pglistalloc waiting\n");
497 1.20 drochner #endif
498 1.20 drochner uvm_wait("pglalloc");
499 1.20 drochner goto again;
500 1.20 drochner } else
501 1.20 drochner uvm_pglistfree(rlist);
502 1.20 drochner }
503 1.24 drochner #ifdef PGALLOC_VERBOSE
504 1.22 drochner if (!error)
505 1.44 reinoud printf("pgalloc: %"PRIxMAX"..%"PRIxMAX"\n",
506 1.44 reinoud (uintmax_t) VM_PAGE_TO_PHYS(TAILQ_FIRST(rlist)),
507 1.44 reinoud (uintmax_t) VM_PAGE_TO_PHYS(TAILQ_LAST(rlist, pglist)));
508 1.22 drochner #endif
509 1.3 mrg return (error);
510 1.20 drochner }
511 1.20 drochner
512 1.20 drochner int
513 1.33 thorpej uvm_pglistalloc(psize_t size, paddr_t low, paddr_t high, paddr_t alignment,
514 1.33 thorpej paddr_t boundary, struct pglist *rlist, int nsegs, int waitok)
515 1.20 drochner {
516 1.24 drochner int num, res;
517 1.20 drochner
518 1.81 ad KASSERT(!cpu_intr_p());
519 1.81 ad KASSERT(!cpu_softintr_p());
520 1.20 drochner KASSERT((alignment & (alignment - 1)) == 0);
521 1.20 drochner KASSERT((boundary & (boundary - 1)) == 0);
522 1.20 drochner
523 1.20 drochner /*
524 1.20 drochner * Our allocations are always page granularity, so our alignment
525 1.20 drochner * must be, too.
526 1.20 drochner */
527 1.20 drochner if (alignment < PAGE_SIZE)
528 1.20 drochner alignment = PAGE_SIZE;
529 1.24 drochner if (boundary != 0 && boundary < size)
530 1.24 drochner return (EINVAL);
531 1.24 drochner num = atop(round_page(size));
532 1.52 matt low = roundup2(low, alignment);
533 1.21 drochner
534 1.21 drochner TAILQ_INIT(rlist);
535 1.20 drochner
536 1.78 ad /*
537 1.78 ad * Turn off the caching of free pages - we need everything to be on
538 1.78 ad * the global freelists.
539 1.78 ad */
540 1.78 ad uvm_pgflcache_pause();
541 1.78 ad
542 1.23 enami if ((nsegs < size >> PAGE_SHIFT) || (alignment != PAGE_SIZE) ||
543 1.23 enami (boundary != 0))
544 1.24 drochner res = uvm_pglistalloc_contig(num, low, high, alignment,
545 1.24 drochner boundary, rlist);
546 1.20 drochner else
547 1.24 drochner res = uvm_pglistalloc_simple(num, low, high, rlist, waitok);
548 1.20 drochner
549 1.78 ad uvm_pgflcache_resume();
550 1.78 ad
551 1.20 drochner return (res);
552 1.1 mrg }
553 1.1 mrg
554 1.1 mrg /*
555 1.1 mrg * uvm_pglistfree: free a list of pages
556 1.1 mrg *
557 1.1 mrg * => pages should already be unmapped
558 1.1 mrg */
559 1.1 mrg
560 1.3 mrg void
561 1.33 thorpej uvm_pglistfree(struct pglist *list)
562 1.1 mrg {
563 1.18 chs struct vm_page *pg;
564 1.1 mrg
565 1.81 ad KASSERT(!cpu_intr_p());
566 1.81 ad KASSERT(!cpu_softintr_p());
567 1.81 ad
568 1.18 chs while ((pg = TAILQ_FIRST(list)) != NULL) {
569 1.42 ad TAILQ_REMOVE(list, pg, pageq.queue);
570 1.82 ad uvm_pagefree(pg);
571 1.3 mrg STAT_DECR(uvm_pglistalloc_npages);
572 1.3 mrg }
573 1.1 mrg }
574