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