Home | History | Annotate | Line # | Download | only in uvm
uvm_pglist.c revision 1.42.16.3
      1  1.42.16.3      matt /*	$NetBSD: uvm_pglist.c,v 1.42.16.3 2010/01/22 08:54:41 matt Exp $	*/
      2        1.2   thorpej 
      3        1.1       mrg /*-
      4        1.1       mrg  * Copyright (c) 1997 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.15       chs  * NASA Ames Research Center.
     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.42.16.3      matt __KERNEL_RCSID(0, "$NetBSD: uvm_pglist.c,v 1.42.16.3 2010/01/22 08:54:41 matt Exp $");
     39        1.1       mrg 
     40        1.1       mrg #include <sys/param.h>
     41        1.1       mrg #include <sys/systm.h>
     42        1.1       mrg #include <sys/malloc.h>
     43        1.1       mrg #include <sys/proc.h>
     44        1.1       mrg 
     45        1.1       mrg #include <uvm/uvm.h>
     46       1.36      yamt #include <uvm/uvm_pdpolicy.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.20  drochner 	int free_list, color, pgflidx;
     85       1.20  drochner 
     86       1.39        ad 	KASSERT(mutex_owned(&uvm_fpageqlock));
     87       1.39        ad 
     88       1.20  drochner #if PGFL_NQUEUES != 2
     89       1.20  drochner #error uvm_pglistalloc needs to be updated
     90       1.20  drochner #endif
     91       1.20  drochner 
     92       1.20  drochner 	free_list = uvm_page_lookup_freelist(pg);
     93       1.20  drochner 	color = VM_PGCOLOR_BUCKET(pg);
     94       1.20  drochner 	pgflidx = (pg->flags & PG_ZERO) ? PGFL_ZEROS : PGFL_UNKNOWN;
     95  1.42.16.1      matt #if defined(DEBUG) && DEBUG > 1
     96  1.42.16.1      matt 	struct vm_page *tp;
     97  1.42.16.1      matt 	LIST_FOREACH(tp,
     98  1.42.16.1      matt 	    &uvm.page_free[free_list].pgfl_buckets[color].pgfl_queues[pgflidx],
     99  1.42.16.1      matt 	    pageq.list) {
    100       1.20  drochner 		if (tp == pg)
    101       1.20  drochner 			break;
    102       1.20  drochner 	}
    103       1.20  drochner 	if (tp == NULL)
    104       1.20  drochner 		panic("uvm_pglistalloc: page not on freelist");
    105       1.20  drochner #endif
    106       1.42        ad 	LIST_REMOVE(pg, pageq.list);	/* global */
    107       1.42        ad 	LIST_REMOVE(pg, listq.list);	/* cpu */
    108       1.20  drochner 	uvmexp.free--;
    109       1.20  drochner 	if (pg->flags & PG_ZERO)
    110       1.20  drochner 		uvmexp.zeropages--;
    111       1.42        ad 	VM_FREE_PAGE_TO_CPU(pg)->pages[pgflidx]--;
    112       1.20  drochner 	pg->flags = PG_CLEAN;
    113       1.20  drochner 	pg->pqflags = 0;
    114       1.20  drochner 	pg->uobject = NULL;
    115       1.20  drochner 	pg->uanon = NULL;
    116       1.42        ad 	TAILQ_INSERT_TAIL(rlist, pg, pageq.queue);
    117       1.20  drochner 	STAT_INCR(uvm_pglistalloc_npages);
    118       1.20  drochner }
    119       1.20  drochner 
    120       1.20  drochner static int
    121       1.33   thorpej uvm_pglistalloc_c_ps(struct vm_physseg *ps, int num, paddr_t low, paddr_t high,
    122       1.33   thorpej     paddr_t alignment, paddr_t boundary, struct pglist *rlist)
    123        1.1       mrg {
    124  1.42.16.1      matt 	signed int try, limit, tryidx, end, idx, skip;
    125  1.42.16.1      matt 	const signed int align = atop(alignment);
    126       1.20  drochner 	struct vm_page *pgs;
    127       1.24  drochner 	int pagemask;
    128       1.24  drochner #ifdef DEBUG
    129       1.22  drochner 	paddr_t idxpa, lastidxpa;
    130       1.35  christos 	int cidx = 0;	/* XXX: GCC */
    131       1.22  drochner #endif
    132       1.24  drochner #ifdef PGALLOC_VERBOSE
    133       1.26   thorpej 	printf("pgalloc: contig %d pgs from psi %ld\n", num,
    134       1.26   thorpej 	(long)(ps - vm_physmem));
    135       1.24  drochner #endif
    136        1.1       mrg 
    137       1.39        ad 	KASSERT(mutex_owned(&uvm_fpageqlock));
    138       1.39        ad 
    139  1.42.16.1      matt 	try = roundup(max(atop(low), ps->avail_start), align);
    140       1.24  drochner 	limit = min(atop(high), ps->avail_end);
    141       1.24  drochner 	pagemask = ~((boundary >> PAGE_SHIFT) - 1);
    142  1.42.16.1      matt 	skip = 0;
    143       1.12       chs 
    144       1.24  drochner 	for (;;) {
    145  1.42.16.1      matt 		bool ok = true;
    146  1.42.16.1      matt 		int cnt;
    147  1.42.16.1      matt 
    148       1.24  drochner 		if (try + num > limit) {
    149        1.3       mrg 			/*
    150        1.3       mrg 			 * We've run past the allowable range.
    151        1.3       mrg 			 */
    152       1.22  drochner 			return (0); /* FAIL */
    153        1.3       mrg 		}
    154       1.24  drochner 		if (boundary != 0 &&
    155       1.24  drochner 		    ((try ^ (try + num - 1)) & pagemask) != 0) {
    156       1.24  drochner 			/*
    157       1.24  drochner 			 * Region crosses boundary. Jump to the boundary
    158       1.24  drochner 			 * just crossed and ensure alignment.
    159       1.24  drochner 			 */
    160       1.24  drochner 			try = (try + num - 1) & pagemask;
    161  1.42.16.1      matt 			try = roundup(try, align);
    162       1.24  drochner 			continue;
    163       1.24  drochner 		}
    164       1.22  drochner #ifdef DEBUG
    165        1.3       mrg 		/*
    166        1.3       mrg 		 * Make sure this is a managed physical page.
    167        1.3       mrg 		 */
    168        1.3       mrg 
    169       1.24  drochner 		if (vm_physseg_find(try, &cidx) != ps - vm_physmem)
    170       1.22  drochner 			panic("pgalloc contig: botch1");
    171       1.24  drochner 		if (cidx != try - ps->start)
    172       1.22  drochner 			panic("pgalloc contig: botch2");
    173       1.24  drochner 		if (vm_physseg_find(try + num - 1, &cidx) != ps - vm_physmem)
    174       1.22  drochner 			panic("pgalloc contig: botch3");
    175       1.24  drochner 		if (cidx != try - ps->start + num - 1)
    176       1.31  junyoung 			panic("pgalloc contig: botch4");
    177       1.22  drochner #endif
    178       1.24  drochner 		tryidx = try - ps->start;
    179       1.24  drochner 		end = tryidx + num;
    180       1.24  drochner 		pgs = ps->pgs;
    181        1.3       mrg 
    182        1.3       mrg 		/*
    183       1.24  drochner 		 * Found a suitable starting page.  See if the range is free.
    184        1.3       mrg 		 */
    185  1.42.16.1      matt #ifdef PGALLOC_VERBOSE
    186  1.42.16.1      matt 		printf("%s: ps=%p try=%#x end=%#x skip=%#x, align=%#x",
    187  1.42.16.1      matt 		    __func__, ps, tryidx, end, skip, align);
    188  1.42.16.1      matt #endif
    189  1.42.16.1      matt 		/*
    190  1.42.16.3      matt 		 * We start at the end and work backwards since if we find a
    191  1.42.16.3      matt 		 * non-free page, it makes no sense to continue.
    192  1.42.16.1      matt 		 *
    193  1.42.16.3      matt 		 * But on the plus size we have "vetted" some number of free
    194  1.42.16.3      matt 		 * pages.  If this iteration fails, we may be able to skip
    195  1.42.16.3      matt 		 * testing most of those pages again in the next pass.
    196  1.42.16.1      matt 		 */
    197  1.42.16.3      matt 		for (idx = end - 1; idx >= tryidx + skip; idx--) {
    198  1.42.16.1      matt 			if (VM_PAGE_IS_FREE(&pgs[idx]) == 0) {
    199  1.42.16.1      matt 				ok = false;
    200        1.3       mrg 				break;
    201  1.42.16.1      matt 			}
    202       1.24  drochner 
    203       1.24  drochner #ifdef DEBUG
    204        1.3       mrg 			if (idx > tryidx) {
    205  1.42.16.3      matt 				idxpa = VM_PAGE_TO_PHYS(&pgs[idx]);
    206        1.3       mrg 				lastidxpa = VM_PAGE_TO_PHYS(&pgs[idx - 1]);
    207       1.12       chs 				if ((lastidxpa + PAGE_SIZE) != idxpa) {
    208        1.3       mrg 					/*
    209        1.3       mrg 					 * Region not contiguous.
    210        1.3       mrg 					 */
    211       1.22  drochner 					panic("pgalloc contig: botch5");
    212        1.3       mrg 				}
    213        1.3       mrg 				if (boundary != 0 &&
    214       1.24  drochner 				    ((lastidxpa ^ idxpa) & ~(boundary - 1))
    215       1.24  drochner 				    != 0) {
    216        1.3       mrg 					/*
    217        1.3       mrg 					 * Region crosses boundary.
    218        1.3       mrg 					 */
    219       1.24  drochner 					panic("pgalloc contig: botch6");
    220        1.3       mrg 				}
    221        1.3       mrg 			}
    222       1.24  drochner #endif
    223        1.3       mrg 		}
    224  1.42.16.1      matt 
    225  1.42.16.1      matt 		if (ok) {
    226  1.42.16.1      matt 			while (skip-- > 0) {
    227  1.42.16.1      matt 				KDASSERT(VM_PAGE_IS_FREE(&pgs[tryidx + skip]));
    228  1.42.16.1      matt 			}
    229  1.42.16.1      matt #ifdef PGALLOC_VERBOSE
    230  1.42.16.1      matt 			printf(": ok\n");
    231  1.42.16.1      matt #endif
    232        1.3       mrg 			break;
    233  1.42.16.1      matt 		}
    234       1.24  drochner 
    235  1.42.16.1      matt #ifdef PGALLOC_VERBOSE
    236  1.42.16.1      matt 		printf(": non-free at %#x\n", idx - tryidx);
    237  1.42.16.1      matt #endif
    238  1.42.16.1      matt 		/*
    239  1.42.16.1      matt 		 * count the number of pages we can advance
    240  1.42.16.1      matt 		 * since we know they aren't all free.
    241  1.42.16.1      matt 		 */
    242  1.42.16.1      matt 		cnt = idx + 1 - tryidx;
    243  1.42.16.1      matt 		/*
    244  1.42.16.1      matt 		 * now round up that to the needed alignment.
    245  1.42.16.1      matt 		 */
    246  1.42.16.1      matt 		cnt = roundup(cnt, align);
    247  1.42.16.1      matt 		/*
    248  1.42.16.1      matt 		 * The number of pages we can skip checking
    249  1.42.16.2       snj 		 * (might be 0 if cnt > num).
    250  1.42.16.1      matt 		 */
    251  1.42.16.1      matt 		skip = max(num - cnt, 0);
    252  1.42.16.1      matt 		try += cnt;
    253        1.1       mrg 	}
    254        1.1       mrg 
    255        1.3       mrg 	/*
    256        1.3       mrg 	 * we have a chunk of memory that conforms to the requested constraints.
    257        1.3       mrg 	 */
    258  1.42.16.1      matt 	for (idx = tryidx, pgs += idx; idx < end; idx++, pgs++)
    259  1.42.16.1      matt 		uvm_pglist_add(pgs, rlist);
    260       1.24  drochner 
    261       1.24  drochner #ifdef PGALLOC_VERBOSE
    262       1.24  drochner 	printf("got %d pgs\n", num);
    263       1.24  drochner #endif
    264       1.24  drochner 	return (num); /* number of pages allocated */
    265       1.22  drochner }
    266       1.22  drochner 
    267       1.22  drochner static int
    268       1.33   thorpej uvm_pglistalloc_contig(int num, paddr_t low, paddr_t high, paddr_t alignment,
    269       1.33   thorpej     paddr_t boundary, struct pglist *rlist)
    270       1.22  drochner {
    271       1.22  drochner 	int fl, psi;
    272       1.24  drochner 	struct vm_physseg *ps;
    273       1.38        ad 	int error;
    274       1.22  drochner 
    275       1.22  drochner 	/* Default to "lose". */
    276       1.22  drochner 	error = ENOMEM;
    277       1.22  drochner 
    278       1.22  drochner 	/*
    279       1.22  drochner 	 * Block all memory allocation and lock the free list.
    280       1.22  drochner 	 */
    281       1.38        ad 	mutex_spin_enter(&uvm_fpageqlock);
    282       1.22  drochner 
    283       1.22  drochner 	/* Are there even any free pages? */
    284       1.22  drochner 	if (uvmexp.free <= (uvmexp.reserve_pagedaemon + uvmexp.reserve_kernel))
    285       1.22  drochner 		goto out;
    286       1.22  drochner 
    287       1.22  drochner 	for (fl = 0; fl < VM_NFREELIST; fl++) {
    288       1.22  drochner #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
    289       1.22  drochner 		for (psi = vm_nphysseg - 1 ; psi >= 0 ; psi--)
    290       1.22  drochner #else
    291       1.22  drochner 		for (psi = 0 ; psi < vm_nphysseg ; psi++)
    292       1.22  drochner #endif
    293       1.22  drochner 		{
    294       1.24  drochner 			ps = &vm_physmem[psi];
    295       1.24  drochner 
    296       1.24  drochner 			if (ps->free_list != fl)
    297       1.22  drochner 				continue;
    298       1.22  drochner 
    299       1.24  drochner 			num -= uvm_pglistalloc_c_ps(ps, num, low, high,
    300       1.24  drochner 						    alignment, boundary, rlist);
    301       1.24  drochner 			if (num == 0) {
    302       1.24  drochner #ifdef PGALLOC_VERBOSE
    303       1.22  drochner 				printf("pgalloc: %lx-%lx\n",
    304       1.28      yamt 				       VM_PAGE_TO_PHYS(TAILQ_FIRST(rlist)),
    305       1.28      yamt 				       VM_PAGE_TO_PHYS(TAILQ_LAST(rlist)));
    306       1.22  drochner #endif
    307       1.22  drochner 				error = 0;
    308       1.22  drochner 				goto out;
    309       1.22  drochner 			}
    310       1.22  drochner 		}
    311       1.22  drochner 	}
    312       1.20  drochner 
    313       1.20  drochner out:
    314       1.20  drochner 	/*
    315       1.20  drochner 	 * check to see if we need to generate some free pages waking
    316       1.20  drochner 	 * the pagedaemon.
    317       1.20  drochner 	 */
    318       1.20  drochner 
    319       1.36      yamt 	uvm_kick_pdaemon();
    320       1.38        ad 	mutex_spin_exit(&uvm_fpageqlock);
    321       1.20  drochner 	return (error);
    322       1.20  drochner }
    323       1.20  drochner 
    324       1.24  drochner static int
    325       1.33   thorpej uvm_pglistalloc_s_ps(struct vm_physseg *ps, int num, paddr_t low, paddr_t high,
    326       1.33   thorpej     struct pglist *rlist)
    327       1.22  drochner {
    328       1.24  drochner 	int todo, limit, try;
    329       1.22  drochner 	struct vm_page *pg;
    330       1.22  drochner #ifdef DEBUG
    331       1.35  christos 	int cidx = 0;	/* XXX: GCC */
    332       1.22  drochner #endif
    333       1.24  drochner #ifdef PGALLOC_VERBOSE
    334       1.26   thorpej 	printf("pgalloc: simple %d pgs from psi %ld\n", num,
    335       1.26   thorpej 	    (long)(ps - vm_physmem));
    336       1.24  drochner #endif
    337       1.22  drochner 
    338       1.39        ad 	KASSERT(mutex_owned(&uvm_fpageqlock));
    339       1.39        ad 
    340       1.24  drochner 	todo = num;
    341       1.24  drochner 	limit = min(atop(high), ps->avail_end);
    342       1.22  drochner 
    343       1.24  drochner 	for (try = max(atop(low), ps->avail_start);
    344       1.22  drochner 	     try < limit; try ++) {
    345       1.22  drochner #ifdef DEBUG
    346       1.24  drochner 		if (vm_physseg_find(try, &cidx) != ps - vm_physmem)
    347       1.22  drochner 			panic("pgalloc simple: botch1");
    348       1.24  drochner 		if (cidx != (try - ps->start))
    349       1.22  drochner 			panic("pgalloc simple: botch2");
    350       1.22  drochner #endif
    351       1.24  drochner 		pg = &ps->pgs[try - ps->start];
    352       1.22  drochner 		if (VM_PAGE_IS_FREE(pg) == 0)
    353       1.22  drochner 			continue;
    354       1.22  drochner 
    355       1.22  drochner 		uvm_pglist_add(pg, rlist);
    356       1.24  drochner 		if (--todo == 0)
    357       1.22  drochner 			break;
    358       1.22  drochner 	}
    359       1.24  drochner 
    360       1.24  drochner #ifdef PGALLOC_VERBOSE
    361       1.24  drochner 	printf("got %d pgs\n", num - todo);
    362       1.24  drochner #endif
    363       1.24  drochner 	return (num - todo); /* number of pages allocated */
    364       1.22  drochner }
    365       1.22  drochner 
    366       1.20  drochner static int
    367       1.33   thorpej uvm_pglistalloc_simple(int num, paddr_t low, paddr_t high,
    368       1.33   thorpej     struct pglist *rlist, int waitok)
    369       1.20  drochner {
    370       1.38        ad 	int fl, psi, error;
    371       1.24  drochner 	struct vm_physseg *ps;
    372       1.20  drochner 
    373       1.20  drochner 	/* Default to "lose". */
    374       1.20  drochner 	error = ENOMEM;
    375       1.20  drochner 
    376       1.20  drochner again:
    377       1.20  drochner 	/*
    378       1.20  drochner 	 * Block all memory allocation and lock the free list.
    379       1.20  drochner 	 */
    380       1.38        ad 	mutex_spin_enter(&uvm_fpageqlock);
    381       1.20  drochner 
    382       1.20  drochner 	/* Are there even any free pages? */
    383       1.20  drochner 	if (uvmexp.free <= (uvmexp.reserve_pagedaemon + uvmexp.reserve_kernel))
    384       1.20  drochner 		goto out;
    385       1.20  drochner 
    386       1.22  drochner 	for (fl = 0; fl < VM_NFREELIST; fl++) {
    387       1.22  drochner #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
    388       1.22  drochner 		for (psi = vm_nphysseg - 1 ; psi >= 0 ; psi--)
    389       1.22  drochner #else
    390       1.22  drochner 		for (psi = 0 ; psi < vm_nphysseg ; psi++)
    391       1.22  drochner #endif
    392       1.22  drochner 		{
    393       1.24  drochner 			ps = &vm_physmem[psi];
    394       1.24  drochner 
    395       1.24  drochner 			if (ps->free_list != fl)
    396       1.22  drochner 				continue;
    397       1.22  drochner 
    398       1.24  drochner 			num -= uvm_pglistalloc_s_ps(ps, num, low, high, rlist);
    399       1.24  drochner 			if (num == 0) {
    400       1.22  drochner 				error = 0;
    401       1.22  drochner 				goto out;
    402       1.22  drochner 			}
    403       1.22  drochner 		}
    404       1.20  drochner 
    405        1.3       mrg 	}
    406        1.1       mrg 
    407        1.1       mrg out:
    408        1.3       mrg 	/*
    409        1.3       mrg 	 * check to see if we need to generate some free pages waking
    410        1.3       mrg 	 * the pagedaemon.
    411        1.3       mrg 	 */
    412       1.15       chs 
    413       1.36      yamt 	uvm_kick_pdaemon();
    414       1.38        ad 	mutex_spin_exit(&uvm_fpageqlock);
    415       1.38        ad 
    416       1.20  drochner 	if (error) {
    417       1.20  drochner 		if (waitok) {
    418       1.20  drochner 			/* XXX perhaps some time limitation? */
    419       1.20  drochner #ifdef DEBUG
    420       1.20  drochner 			printf("pglistalloc waiting\n");
    421       1.20  drochner #endif
    422       1.20  drochner 			uvm_wait("pglalloc");
    423       1.20  drochner 			goto again;
    424       1.20  drochner 		} else
    425       1.20  drochner 			uvm_pglistfree(rlist);
    426       1.20  drochner 	}
    427       1.24  drochner #ifdef PGALLOC_VERBOSE
    428       1.22  drochner 	if (!error)
    429       1.22  drochner 		printf("pgalloc: %lx..%lx\n",
    430       1.28      yamt 		       VM_PAGE_TO_PHYS(TAILQ_FIRST(rlist)),
    431       1.28      yamt 		       VM_PAGE_TO_PHYS(TAILQ_LAST(rlist, pglist)));
    432       1.22  drochner #endif
    433        1.3       mrg 	return (error);
    434       1.20  drochner }
    435       1.20  drochner 
    436       1.20  drochner int
    437       1.33   thorpej uvm_pglistalloc(psize_t size, paddr_t low, paddr_t high, paddr_t alignment,
    438       1.33   thorpej     paddr_t boundary, struct pglist *rlist, int nsegs, int waitok)
    439       1.20  drochner {
    440       1.24  drochner 	int num, res;
    441       1.20  drochner 
    442       1.20  drochner 	KASSERT((alignment & (alignment - 1)) == 0);
    443       1.20  drochner 	KASSERT((boundary & (boundary - 1)) == 0);
    444       1.20  drochner 
    445       1.20  drochner 	/*
    446       1.20  drochner 	 * Our allocations are always page granularity, so our alignment
    447       1.20  drochner 	 * must be, too.
    448       1.20  drochner 	 */
    449       1.20  drochner 	if (alignment < PAGE_SIZE)
    450       1.20  drochner 		alignment = PAGE_SIZE;
    451       1.24  drochner 	if (boundary != 0 && boundary < size)
    452       1.24  drochner 		return (EINVAL);
    453       1.24  drochner 	num = atop(round_page(size));
    454       1.20  drochner 	low = roundup(low, alignment);
    455       1.21  drochner 
    456       1.21  drochner 	TAILQ_INIT(rlist);
    457       1.20  drochner 
    458       1.23     enami 	if ((nsegs < size >> PAGE_SHIFT) || (alignment != PAGE_SIZE) ||
    459       1.23     enami 	    (boundary != 0))
    460       1.24  drochner 		res = uvm_pglistalloc_contig(num, low, high, alignment,
    461       1.24  drochner 					     boundary, rlist);
    462       1.20  drochner 	else
    463       1.24  drochner 		res = uvm_pglistalloc_simple(num, low, high, rlist, waitok);
    464       1.20  drochner 
    465       1.20  drochner 	return (res);
    466        1.1       mrg }
    467        1.1       mrg 
    468        1.1       mrg /*
    469        1.1       mrg  * uvm_pglistfree: free a list of pages
    470        1.1       mrg  *
    471        1.1       mrg  * => pages should already be unmapped
    472        1.1       mrg  */
    473        1.1       mrg 
    474        1.3       mrg void
    475       1.33   thorpej uvm_pglistfree(struct pglist *list)
    476        1.1       mrg {
    477       1.42        ad 	struct uvm_cpu *ucpu;
    478       1.18       chs 	struct vm_page *pg;
    479       1.42        ad 	int index, color, queue;
    480       1.42        ad 	bool iszero;
    481        1.1       mrg 
    482        1.3       mrg 	/*
    483       1.18       chs 	 * Lock the free list and free each page.
    484        1.3       mrg 	 */
    485       1.18       chs 
    486       1.38        ad 	mutex_spin_enter(&uvm_fpageqlock);
    487       1.42        ad 	ucpu = curcpu()->ci_data.cpu_uvm;
    488       1.18       chs 	while ((pg = TAILQ_FIRST(list)) != NULL) {
    489       1.36      yamt 		KASSERT(!uvmpdpol_pageisqueued_p(pg));
    490       1.42        ad 		TAILQ_REMOVE(list, pg, pageq.queue);
    491       1.29      yamt 		iszero = (pg->flags & PG_ZERO);
    492       1.18       chs 		pg->pqflags = PQ_FREE;
    493       1.29      yamt #ifdef DEBUG
    494       1.29      yamt 		pg->uobject = (void *)0xdeadbeef;
    495       1.29      yamt 		pg->uanon = (void *)0xdeadbeef;
    496       1.30      yamt #endif /* DEBUG */
    497       1.30      yamt #ifdef DEBUG
    498       1.30      yamt 		if (iszero)
    499       1.30      yamt 			uvm_pagezerocheck(pg);
    500       1.30      yamt #endif /* DEBUG */
    501       1.42        ad 		index = uvm_page_lookup_freelist(pg);
    502       1.42        ad 		color = VM_PGCOLOR_BUCKET(pg);
    503       1.42        ad 		queue = iszero ? PGFL_ZEROS : PGFL_UNKNOWN;
    504       1.42        ad 		pg->offset = (uintptr_t)ucpu;
    505       1.42        ad 		LIST_INSERT_HEAD(&uvm.page_free[index].pgfl_buckets[color].
    506       1.42        ad 		    pgfl_queues[queue], pg, pageq.list);
    507       1.42        ad 		LIST_INSERT_HEAD(&ucpu->page_free[index].pgfl_buckets[color].
    508       1.42        ad 		    pgfl_queues[queue], pg, listq.list);
    509        1.3       mrg 		uvmexp.free++;
    510       1.29      yamt 		if (iszero)
    511       1.29      yamt 			uvmexp.zeropages++;
    512       1.42        ad 		ucpu->pages[queue]++;
    513        1.3       mrg 		STAT_DECR(uvm_pglistalloc_npages);
    514        1.3       mrg 	}
    515       1.42        ad 	if (ucpu->pages[PGFL_ZEROS] < ucpu->pages[PGFL_UNKNOWN])
    516       1.42        ad 		ucpu->page_idle_zero = vm_page_zero_enable;
    517       1.38        ad 	mutex_spin_exit(&uvm_fpageqlock);
    518        1.1       mrg }
    519