Home | History | Annotate | Line # | Download | only in uvm
uvm_pglist.c revision 1.26.2.2
      1  1.26.2.2     skrll /*	$NetBSD: uvm_pglist.c,v 1.26.2.2 2004/09/18 14:57:12 skrll 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.1       mrg  * 3. All advertising materials mentioning features or use of this software
     20       1.1       mrg  *    must display the following acknowledgement:
     21       1.1       mrg  *      This product includes software developed by the NetBSD
     22       1.1       mrg  *      Foundation, Inc. and its contributors.
     23       1.1       mrg  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24       1.1       mrg  *    contributors may be used to endorse or promote products derived
     25       1.1       mrg  *    from this software without specific prior written permission.
     26      1.15       chs  *
     27       1.1       mrg  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28       1.1       mrg  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29       1.1       mrg  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30       1.1       mrg  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31       1.1       mrg  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32       1.1       mrg  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33       1.1       mrg  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34       1.1       mrg  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35       1.1       mrg  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36       1.1       mrg  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37       1.1       mrg  * POSSIBILITY OF SUCH DAMAGE.
     38       1.1       mrg  */
     39       1.1       mrg 
     40       1.1       mrg /*
     41       1.1       mrg  * uvm_pglist.c: pglist functions
     42       1.1       mrg  */
     43      1.19     lukem 
     44      1.19     lukem #include <sys/cdefs.h>
     45  1.26.2.2     skrll __KERNEL_RCSID(0, "$NetBSD: uvm_pglist.c,v 1.26.2.2 2004/09/18 14:57:12 skrll Exp $");
     46       1.1       mrg 
     47       1.1       mrg #include <sys/param.h>
     48       1.1       mrg #include <sys/systm.h>
     49       1.1       mrg #include <sys/malloc.h>
     50       1.1       mrg #include <sys/proc.h>
     51       1.1       mrg 
     52       1.1       mrg #include <uvm/uvm.h>
     53       1.1       mrg 
     54       1.1       mrg #ifdef VM_PAGE_ALLOC_MEMORY_STATS
     55       1.1       mrg #define	STAT_INCR(v)	(v)++
     56       1.1       mrg #define	STAT_DECR(v)	do { \
     57       1.1       mrg 		if ((v) == 0) \
     58       1.1       mrg 			printf("%s:%d -- Already 0!\n", __FILE__, __LINE__); \
     59       1.1       mrg 		else \
     60       1.1       mrg 			(v)--; \
     61      1.25     perry 	} while (/*CONSTCOND*/ 0)
     62       1.1       mrg u_long	uvm_pglistalloc_npages;
     63       1.1       mrg #else
     64       1.1       mrg #define	STAT_INCR(v)
     65       1.1       mrg #define	STAT_DECR(v)
     66       1.1       mrg #endif
     67       1.1       mrg 
     68       1.1       mrg /*
     69       1.1       mrg  * uvm_pglistalloc: allocate a list of pages
     70       1.1       mrg  *
     71  1.26.2.1     skrll  * => allocated pages are placed onto an rlist.  rlist is
     72  1.26.2.1     skrll  *    initialized by uvm_pglistalloc.
     73       1.1       mrg  * => returns 0 on success or errno on failure
     74  1.26.2.1     skrll  * => implementation allocates a single segment if any constraints are
     75  1.26.2.1     skrll  *	imposed by call arguments.
     76       1.1       mrg  * => doesn't take into account clean non-busy pages on inactive list
     77       1.1       mrg  *	that could be used(?)
     78       1.1       mrg  * => params:
     79       1.1       mrg  *	size		the size of the allocation, rounded to page size.
     80       1.1       mrg  *	low		the low address of the allowed allocation range.
     81       1.1       mrg  *	high		the high address of the allowed allocation range.
     82       1.1       mrg  *	alignment	memory must be aligned to this power-of-two boundary.
     83      1.15       chs  *	boundary	no segment in the allocation may cross this
     84       1.1       mrg  *			power-of-two boundary (relative to zero).
     85       1.1       mrg  */
     86       1.1       mrg 
     87      1.20  drochner static void uvm_pglist_add(struct vm_page *, struct pglist *);
     88      1.24  drochner static int uvm_pglistalloc_c_ps(struct vm_physseg *, int, paddr_t, paddr_t,
     89      1.22  drochner 				paddr_t, paddr_t, struct pglist *);
     90      1.24  drochner static int uvm_pglistalloc_contig(int, paddr_t, paddr_t, paddr_t, paddr_t,
     91      1.20  drochner 				  struct pglist *);
     92      1.24  drochner static int uvm_pglistalloc_s_ps(struct vm_physseg *, int, paddr_t, paddr_t,
     93      1.24  drochner 				struct pglist *);
     94      1.24  drochner static int uvm_pglistalloc_simple(int, paddr_t, paddr_t,
     95      1.20  drochner 				  struct pglist *, int);
     96      1.20  drochner 
     97      1.20  drochner static void
     98      1.20  drochner uvm_pglist_add(pg, rlist)
     99      1.20  drochner 	struct vm_page *pg;
    100      1.20  drochner 	struct pglist *rlist;
    101      1.20  drochner {
    102      1.20  drochner 	int free_list, color, pgflidx;
    103      1.20  drochner #ifdef DEBUG
    104      1.20  drochner 	struct vm_page *tp;
    105      1.20  drochner #endif
    106      1.20  drochner 
    107      1.20  drochner #if PGFL_NQUEUES != 2
    108      1.20  drochner #error uvm_pglistalloc needs to be updated
    109      1.20  drochner #endif
    110      1.20  drochner 
    111      1.20  drochner 	free_list = uvm_page_lookup_freelist(pg);
    112      1.20  drochner 	color = VM_PGCOLOR_BUCKET(pg);
    113      1.20  drochner 	pgflidx = (pg->flags & PG_ZERO) ? PGFL_ZEROS : PGFL_UNKNOWN;
    114      1.20  drochner #ifdef DEBUG
    115      1.20  drochner 	for (tp = TAILQ_FIRST(&uvm.page_free[
    116      1.20  drochner 		free_list].pgfl_buckets[color].pgfl_queues[pgflidx]);
    117      1.20  drochner 	     tp != NULL;
    118      1.20  drochner 	     tp = TAILQ_NEXT(tp, pageq)) {
    119      1.20  drochner 		if (tp == pg)
    120      1.20  drochner 			break;
    121      1.20  drochner 	}
    122      1.20  drochner 	if (tp == NULL)
    123      1.20  drochner 		panic("uvm_pglistalloc: page not on freelist");
    124      1.20  drochner #endif
    125      1.20  drochner 	TAILQ_REMOVE(&uvm.page_free[free_list].pgfl_buckets[
    126      1.20  drochner 			color].pgfl_queues[pgflidx], pg, pageq);
    127      1.20  drochner 	uvmexp.free--;
    128      1.20  drochner 	if (pg->flags & PG_ZERO)
    129      1.20  drochner 		uvmexp.zeropages--;
    130      1.20  drochner 	pg->flags = PG_CLEAN;
    131      1.20  drochner 	pg->pqflags = 0;
    132      1.20  drochner 	pg->uobject = NULL;
    133      1.20  drochner 	pg->uanon = NULL;
    134      1.20  drochner 	TAILQ_INSERT_TAIL(rlist, pg, pageq);
    135      1.20  drochner 	STAT_INCR(uvm_pglistalloc_npages);
    136      1.20  drochner }
    137      1.20  drochner 
    138      1.20  drochner static int
    139      1.24  drochner uvm_pglistalloc_c_ps(ps, num, low, high, alignment, boundary, rlist)
    140      1.24  drochner 	struct vm_physseg *ps;
    141      1.24  drochner 	int num;
    142       1.6       eeh 	paddr_t low, high, alignment, boundary;
    143       1.3       mrg 	struct pglist *rlist;
    144       1.1       mrg {
    145      1.22  drochner 	int try, limit, tryidx, end, idx;
    146      1.20  drochner 	struct vm_page *pgs;
    147      1.24  drochner 	int pagemask;
    148      1.24  drochner #ifdef DEBUG
    149      1.22  drochner 	paddr_t idxpa, lastidxpa;
    150      1.22  drochner 	int cidx;
    151      1.22  drochner #endif
    152      1.24  drochner #ifdef PGALLOC_VERBOSE
    153      1.26   thorpej 	printf("pgalloc: contig %d pgs from psi %ld\n", num,
    154      1.26   thorpej 	(long)(ps - vm_physmem));
    155      1.24  drochner #endif
    156       1.1       mrg 
    157      1.24  drochner 	try = roundup(max(atop(low), ps->avail_start), atop(alignment));
    158      1.24  drochner 	limit = min(atop(high), ps->avail_end);
    159      1.24  drochner 	pagemask = ~((boundary >> PAGE_SHIFT) - 1);
    160      1.12       chs 
    161      1.24  drochner 	for (;;) {
    162      1.24  drochner 		if (try + num > limit) {
    163       1.3       mrg 			/*
    164       1.3       mrg 			 * We've run past the allowable range.
    165       1.3       mrg 			 */
    166      1.22  drochner 			return (0); /* FAIL */
    167       1.3       mrg 		}
    168      1.24  drochner 		if (boundary != 0 &&
    169      1.24  drochner 		    ((try ^ (try + num - 1)) & pagemask) != 0) {
    170      1.24  drochner 			/*
    171      1.24  drochner 			 * Region crosses boundary. Jump to the boundary
    172      1.24  drochner 			 * just crossed and ensure alignment.
    173      1.24  drochner 			 */
    174      1.24  drochner 			try = (try + num - 1) & pagemask;
    175      1.24  drochner 			try = roundup(try, atop(alignment));
    176      1.24  drochner 			continue;
    177      1.24  drochner 		}
    178      1.22  drochner #ifdef DEBUG
    179       1.3       mrg 		/*
    180       1.3       mrg 		 * Make sure this is a managed physical page.
    181       1.3       mrg 		 */
    182       1.3       mrg 
    183      1.24  drochner 		if (vm_physseg_find(try, &cidx) != ps - vm_physmem)
    184      1.22  drochner 			panic("pgalloc contig: botch1");
    185      1.24  drochner 		if (cidx != try - ps->start)
    186      1.22  drochner 			panic("pgalloc contig: botch2");
    187      1.24  drochner 		if (vm_physseg_find(try + num - 1, &cidx) != ps - vm_physmem)
    188      1.22  drochner 			panic("pgalloc contig: botch3");
    189      1.24  drochner 		if (cidx != try - ps->start + num - 1)
    190  1.26.2.1     skrll 			panic("pgalloc contig: botch4");
    191      1.22  drochner #endif
    192      1.24  drochner 		tryidx = try - ps->start;
    193      1.24  drochner 		end = tryidx + num;
    194      1.24  drochner 		pgs = ps->pgs;
    195       1.3       mrg 
    196       1.3       mrg 		/*
    197      1.24  drochner 		 * Found a suitable starting page.  See if the range is free.
    198       1.3       mrg 		 */
    199      1.22  drochner 		for (idx = tryidx; idx < end; idx++) {
    200      1.24  drochner 			if (VM_PAGE_IS_FREE(&pgs[idx]) == 0)
    201       1.3       mrg 				break;
    202      1.24  drochner 
    203      1.24  drochner #ifdef DEBUG
    204       1.3       mrg 			idxpa = VM_PAGE_TO_PHYS(&pgs[idx]);
    205       1.3       mrg 			if (idx > tryidx) {
    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.24  drochner 		if (idx == end)
    225       1.3       mrg 			break;
    226      1.24  drochner 
    227      1.24  drochner 		try += atop(alignment);
    228       1.1       mrg 	}
    229       1.1       mrg 
    230       1.3       mrg 	/*
    231       1.3       mrg 	 * we have a chunk of memory that conforms to the requested constraints.
    232       1.3       mrg 	 */
    233       1.3       mrg 	idx = tryidx;
    234      1.24  drochner 	while (idx < end)
    235      1.20  drochner 		uvm_pglist_add(&pgs[idx++], rlist);
    236      1.24  drochner 
    237      1.24  drochner #ifdef PGALLOC_VERBOSE
    238      1.24  drochner 	printf("got %d pgs\n", num);
    239      1.24  drochner #endif
    240      1.24  drochner 	return (num); /* number of pages allocated */
    241      1.22  drochner }
    242      1.22  drochner 
    243      1.22  drochner static int
    244      1.24  drochner uvm_pglistalloc_contig(num, low, high, alignment, boundary, rlist)
    245      1.24  drochner 	int num;
    246      1.22  drochner 	paddr_t low, high, alignment, boundary;
    247      1.22  drochner 	struct pglist *rlist;
    248      1.22  drochner {
    249      1.22  drochner 	int fl, psi;
    250      1.24  drochner 	struct vm_physseg *ps;
    251      1.22  drochner 	int s, error;
    252      1.22  drochner 
    253      1.22  drochner 	/* Default to "lose". */
    254      1.22  drochner 	error = ENOMEM;
    255      1.22  drochner 
    256      1.22  drochner 	/*
    257      1.22  drochner 	 * Block all memory allocation and lock the free list.
    258      1.22  drochner 	 */
    259      1.22  drochner 	s = uvm_lock_fpageq();
    260      1.22  drochner 
    261      1.22  drochner 	/* Are there even any free pages? */
    262      1.22  drochner 	if (uvmexp.free <= (uvmexp.reserve_pagedaemon + uvmexp.reserve_kernel))
    263      1.22  drochner 		goto out;
    264      1.22  drochner 
    265      1.22  drochner 	for (fl = 0; fl < VM_NFREELIST; fl++) {
    266      1.22  drochner #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
    267      1.22  drochner 		for (psi = vm_nphysseg - 1 ; psi >= 0 ; psi--)
    268      1.22  drochner #else
    269      1.22  drochner 		for (psi = 0 ; psi < vm_nphysseg ; psi++)
    270      1.22  drochner #endif
    271      1.22  drochner 		{
    272      1.24  drochner 			ps = &vm_physmem[psi];
    273      1.24  drochner 
    274      1.24  drochner 			if (ps->free_list != fl)
    275      1.22  drochner 				continue;
    276      1.22  drochner 
    277      1.24  drochner 			num -= uvm_pglistalloc_c_ps(ps, num, low, high,
    278      1.24  drochner 						    alignment, boundary, rlist);
    279      1.24  drochner 			if (num == 0) {
    280      1.24  drochner #ifdef PGALLOC_VERBOSE
    281      1.22  drochner 				printf("pgalloc: %lx-%lx\n",
    282  1.26.2.1     skrll 				       VM_PAGE_TO_PHYS(TAILQ_FIRST(rlist)),
    283  1.26.2.1     skrll 				       VM_PAGE_TO_PHYS(TAILQ_LAST(rlist)));
    284      1.22  drochner #endif
    285      1.22  drochner 				error = 0;
    286      1.22  drochner 				goto out;
    287      1.22  drochner 			}
    288      1.22  drochner 		}
    289      1.22  drochner 	}
    290      1.20  drochner 
    291      1.20  drochner out:
    292      1.20  drochner 	/*
    293      1.20  drochner 	 * check to see if we need to generate some free pages waking
    294      1.20  drochner 	 * the pagedaemon.
    295      1.20  drochner 	 */
    296      1.20  drochner 
    297      1.20  drochner 	UVM_KICK_PDAEMON();
    298      1.20  drochner 	uvm_unlock_fpageq(s);
    299      1.20  drochner 	return (error);
    300      1.20  drochner }
    301      1.20  drochner 
    302      1.24  drochner static int
    303      1.24  drochner uvm_pglistalloc_s_ps(ps, num, low, high, rlist)
    304      1.24  drochner 	struct vm_physseg *ps;
    305      1.24  drochner 	int num;
    306      1.22  drochner 	paddr_t low, high;
    307      1.22  drochner 	struct pglist *rlist;
    308      1.22  drochner {
    309      1.24  drochner 	int todo, limit, try;
    310      1.22  drochner 	struct vm_page *pg;
    311      1.22  drochner #ifdef DEBUG
    312      1.22  drochner 	int cidx;
    313      1.22  drochner #endif
    314      1.24  drochner #ifdef PGALLOC_VERBOSE
    315      1.26   thorpej 	printf("pgalloc: simple %d pgs from psi %ld\n", num,
    316      1.26   thorpej 	    (long)(ps - vm_physmem));
    317      1.24  drochner #endif
    318      1.22  drochner 
    319      1.24  drochner 	todo = num;
    320      1.24  drochner 	limit = min(atop(high), ps->avail_end);
    321      1.22  drochner 
    322      1.24  drochner 	for (try = max(atop(low), ps->avail_start);
    323      1.22  drochner 	     try < limit; try ++) {
    324      1.22  drochner #ifdef DEBUG
    325      1.24  drochner 		if (vm_physseg_find(try, &cidx) != ps - vm_physmem)
    326      1.22  drochner 			panic("pgalloc simple: botch1");
    327      1.24  drochner 		if (cidx != (try - ps->start))
    328      1.22  drochner 			panic("pgalloc simple: botch2");
    329      1.22  drochner #endif
    330      1.24  drochner 		pg = &ps->pgs[try - ps->start];
    331      1.22  drochner 		if (VM_PAGE_IS_FREE(pg) == 0)
    332      1.22  drochner 			continue;
    333      1.22  drochner 
    334      1.22  drochner 		uvm_pglist_add(pg, rlist);
    335      1.24  drochner 		if (--todo == 0)
    336      1.22  drochner 			break;
    337      1.22  drochner 	}
    338      1.24  drochner 
    339      1.24  drochner #ifdef PGALLOC_VERBOSE
    340      1.24  drochner 	printf("got %d pgs\n", num - todo);
    341      1.24  drochner #endif
    342      1.24  drochner 	return (num - todo); /* number of pages allocated */
    343      1.22  drochner }
    344      1.22  drochner 
    345      1.20  drochner static int
    346      1.24  drochner uvm_pglistalloc_simple(num, low, high, rlist, waitok)
    347      1.24  drochner 	int num;
    348      1.20  drochner 	paddr_t low, high;
    349      1.20  drochner 	struct pglist *rlist;
    350      1.20  drochner 	int waitok;
    351      1.20  drochner {
    352      1.24  drochner 	int fl, psi, s, error;
    353      1.24  drochner 	struct vm_physseg *ps;
    354      1.20  drochner 
    355      1.20  drochner 	/* Default to "lose". */
    356      1.20  drochner 	error = ENOMEM;
    357      1.20  drochner 
    358      1.20  drochner again:
    359      1.20  drochner 	/*
    360      1.20  drochner 	 * Block all memory allocation and lock the free list.
    361      1.20  drochner 	 */
    362      1.20  drochner 	s = uvm_lock_fpageq();
    363      1.20  drochner 
    364      1.20  drochner 	/* Are there even any free pages? */
    365      1.20  drochner 	if (uvmexp.free <= (uvmexp.reserve_pagedaemon + uvmexp.reserve_kernel))
    366      1.20  drochner 		goto out;
    367      1.20  drochner 
    368      1.22  drochner 	for (fl = 0; fl < VM_NFREELIST; fl++) {
    369      1.22  drochner #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
    370      1.22  drochner 		for (psi = vm_nphysseg - 1 ; psi >= 0 ; psi--)
    371      1.22  drochner #else
    372      1.22  drochner 		for (psi = 0 ; psi < vm_nphysseg ; psi++)
    373      1.22  drochner #endif
    374      1.22  drochner 		{
    375      1.24  drochner 			ps = &vm_physmem[psi];
    376      1.24  drochner 
    377      1.24  drochner 			if (ps->free_list != fl)
    378      1.22  drochner 				continue;
    379      1.22  drochner 
    380      1.24  drochner 			num -= uvm_pglistalloc_s_ps(ps, num, low, high, rlist);
    381      1.24  drochner 			if (num == 0) {
    382      1.22  drochner 				error = 0;
    383      1.22  drochner 				goto out;
    384      1.22  drochner 			}
    385      1.22  drochner 		}
    386      1.20  drochner 
    387       1.3       mrg 	}
    388       1.1       mrg 
    389       1.1       mrg out:
    390       1.3       mrg 	/*
    391       1.3       mrg 	 * check to see if we need to generate some free pages waking
    392       1.3       mrg 	 * the pagedaemon.
    393       1.3       mrg 	 */
    394      1.15       chs 
    395      1.17   thorpej 	UVM_KICK_PDAEMON();
    396      1.12       chs 	uvm_unlock_fpageq(s);
    397      1.20  drochner 	if (error) {
    398      1.20  drochner 		if (waitok) {
    399      1.20  drochner 			/* XXX perhaps some time limitation? */
    400      1.20  drochner #ifdef DEBUG
    401      1.20  drochner 			printf("pglistalloc waiting\n");
    402      1.20  drochner #endif
    403      1.20  drochner 			uvm_wait("pglalloc");
    404      1.20  drochner 			goto again;
    405      1.20  drochner 		} else
    406      1.20  drochner 			uvm_pglistfree(rlist);
    407      1.20  drochner 	}
    408      1.24  drochner #ifdef PGALLOC_VERBOSE
    409      1.22  drochner 	if (!error)
    410      1.22  drochner 		printf("pgalloc: %lx..%lx\n",
    411  1.26.2.1     skrll 		       VM_PAGE_TO_PHYS(TAILQ_FIRST(rlist)),
    412  1.26.2.1     skrll 		       VM_PAGE_TO_PHYS(TAILQ_LAST(rlist, pglist)));
    413      1.22  drochner #endif
    414       1.3       mrg 	return (error);
    415      1.20  drochner }
    416      1.20  drochner 
    417      1.20  drochner int
    418      1.20  drochner uvm_pglistalloc(size, low, high, alignment, boundary, rlist, nsegs, waitok)
    419      1.20  drochner 	psize_t size;
    420      1.20  drochner 	paddr_t low, high, alignment, boundary;
    421      1.20  drochner 	struct pglist *rlist;
    422      1.20  drochner 	int nsegs, waitok;
    423      1.20  drochner {
    424      1.24  drochner 	int num, res;
    425      1.20  drochner 
    426      1.20  drochner 	KASSERT((alignment & (alignment - 1)) == 0);
    427      1.20  drochner 	KASSERT((boundary & (boundary - 1)) == 0);
    428      1.20  drochner 
    429      1.20  drochner 	/*
    430      1.20  drochner 	 * Our allocations are always page granularity, so our alignment
    431      1.20  drochner 	 * must be, too.
    432      1.20  drochner 	 */
    433      1.20  drochner 	if (alignment < PAGE_SIZE)
    434      1.20  drochner 		alignment = PAGE_SIZE;
    435      1.24  drochner 	if (boundary != 0 && boundary < size)
    436      1.24  drochner 		return (EINVAL);
    437      1.24  drochner 	num = atop(round_page(size));
    438      1.20  drochner 	low = roundup(low, alignment);
    439      1.21  drochner 
    440      1.21  drochner 	TAILQ_INIT(rlist);
    441      1.20  drochner 
    442      1.23     enami 	if ((nsegs < size >> PAGE_SHIFT) || (alignment != PAGE_SIZE) ||
    443      1.23     enami 	    (boundary != 0))
    444      1.24  drochner 		res = uvm_pglistalloc_contig(num, low, high, alignment,
    445      1.24  drochner 					     boundary, rlist);
    446      1.20  drochner 	else
    447      1.24  drochner 		res = uvm_pglistalloc_simple(num, low, high, rlist, waitok);
    448      1.20  drochner 
    449      1.20  drochner 	return (res);
    450       1.1       mrg }
    451       1.1       mrg 
    452       1.1       mrg /*
    453       1.1       mrg  * uvm_pglistfree: free a list of pages
    454       1.1       mrg  *
    455       1.1       mrg  * => pages should already be unmapped
    456       1.1       mrg  */
    457       1.1       mrg 
    458       1.3       mrg void
    459       1.3       mrg uvm_pglistfree(list)
    460       1.3       mrg 	struct pglist *list;
    461       1.1       mrg {
    462      1.18       chs 	struct vm_page *pg;
    463       1.3       mrg 	int s;
    464       1.1       mrg 
    465       1.3       mrg 	/*
    466      1.18       chs 	 * Lock the free list and free each page.
    467       1.3       mrg 	 */
    468      1.18       chs 
    469       1.7   thorpej 	s = uvm_lock_fpageq();
    470      1.18       chs 	while ((pg = TAILQ_FIRST(list)) != NULL) {
    471  1.26.2.1     skrll 		boolean_t iszero;
    472  1.26.2.1     skrll 
    473      1.18       chs 		KASSERT((pg->pqflags & (PQ_ACTIVE|PQ_INACTIVE)) == 0);
    474      1.18       chs 		TAILQ_REMOVE(list, pg, pageq);
    475  1.26.2.1     skrll 		iszero = (pg->flags & PG_ZERO);
    476      1.18       chs 		pg->pqflags = PQ_FREE;
    477  1.26.2.1     skrll #ifdef DEBUG
    478  1.26.2.1     skrll 		pg->uobject = (void *)0xdeadbeef;
    479  1.26.2.1     skrll 		pg->offset = 0xdeadbeef;
    480  1.26.2.1     skrll 		pg->uanon = (void *)0xdeadbeef;
    481  1.26.2.1     skrll #endif /* DEBUG */
    482  1.26.2.1     skrll #ifdef DEBUG
    483  1.26.2.1     skrll 		if (iszero)
    484  1.26.2.1     skrll 			uvm_pagezerocheck(pg);
    485  1.26.2.1     skrll #endif /* DEBUG */
    486  1.26.2.2     skrll 		TAILQ_INSERT_HEAD(&uvm.page_free[uvm_page_lookup_freelist(pg)].
    487      1.18       chs 		    pgfl_buckets[VM_PGCOLOR_BUCKET(pg)].
    488  1.26.2.1     skrll 		    pgfl_queues[iszero ? PGFL_ZEROS : PGFL_UNKNOWN], pg, pageq);
    489       1.3       mrg 		uvmexp.free++;
    490  1.26.2.1     skrll 		if (iszero)
    491  1.26.2.1     skrll 			uvmexp.zeropages++;
    492       1.9   thorpej 		if (uvmexp.zeropages < UVM_PAGEZERO_TARGET)
    493       1.9   thorpej 			uvm.page_idle_zero = vm_page_zero_enable;
    494       1.3       mrg 		STAT_DECR(uvm_pglistalloc_npages);
    495       1.3       mrg 	}
    496       1.7   thorpej 	uvm_unlock_fpageq(s);
    497       1.1       mrg }
    498