Home | History | Annotate | Line # | Download | only in uvm
uvm_pglist.c revision 1.50
      1  1.50    cegger /*	$NetBSD: uvm_pglist.c,v 1.50 2010/11/18 11:49:41 cegger Exp $	*/
      2  1.45    nonaka 
      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.50    cegger __KERNEL_RCSID(0, "$NetBSD: uvm_pglist.c,v 1.50 2010/11/18 11:49:41 cegger 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.50    cegger #ifdef DEBUG
     63  1.48  uebayasi static int
     64  1.48  uebayasi vm_physmem_index(struct vm_physseg *ps)
     65  1.48  uebayasi {
     66  1.48  uebayasi 	int i;
     67  1.48  uebayasi 
     68  1.48  uebayasi 	for (i = 0; i < vm_nphysmem; i++) {
     69  1.48  uebayasi 		if (VM_PHYSMEM_PTR(i) == ps)
     70  1.48  uebayasi 			return i;
     71  1.48  uebayasi 	}
     72  1.48  uebayasi 	return -1;
     73  1.48  uebayasi }
     74  1.48  uebayasi #endif
     75  1.48  uebayasi 
     76   1.1       mrg /*
     77   1.1       mrg  * uvm_pglistalloc: allocate a list of pages
     78   1.1       mrg  *
     79  1.27  drochner  * => allocated pages are placed onto an rlist.  rlist is
     80  1.27  drochner  *    initialized by uvm_pglistalloc.
     81   1.1       mrg  * => returns 0 on success or errno on failure
     82  1.27  drochner  * => implementation allocates a single segment if any constraints are
     83  1.27  drochner  *	imposed by call arguments.
     84   1.1       mrg  * => doesn't take into account clean non-busy pages on inactive list
     85   1.1       mrg  *	that could be used(?)
     86   1.1       mrg  * => params:
     87   1.1       mrg  *	size		the size of the allocation, rounded to page size.
     88   1.1       mrg  *	low		the low address of the allowed allocation range.
     89   1.1       mrg  *	high		the high address of the allowed allocation range.
     90   1.1       mrg  *	alignment	memory must be aligned to this power-of-two boundary.
     91  1.15       chs  *	boundary	no segment in the allocation may cross this
     92   1.1       mrg  *			power-of-two boundary (relative to zero).
     93   1.1       mrg  */
     94   1.1       mrg 
     95  1.20  drochner static void
     96  1.33   thorpej uvm_pglist_add(struct vm_page *pg, struct pglist *rlist)
     97  1.20  drochner {
     98  1.20  drochner 	int free_list, color, pgflidx;
     99  1.46       mrg #ifdef NOT_DEBUG
    100  1.20  drochner 	struct vm_page *tp;
    101  1.20  drochner #endif
    102  1.20  drochner 
    103  1.39        ad 	KASSERT(mutex_owned(&uvm_fpageqlock));
    104  1.39        ad 
    105  1.20  drochner #if PGFL_NQUEUES != 2
    106  1.20  drochner #error uvm_pglistalloc needs to be updated
    107  1.20  drochner #endif
    108  1.20  drochner 
    109  1.20  drochner 	free_list = uvm_page_lookup_freelist(pg);
    110  1.20  drochner 	color = VM_PGCOLOR_BUCKET(pg);
    111  1.20  drochner 	pgflidx = (pg->flags & PG_ZERO) ? PGFL_ZEROS : PGFL_UNKNOWN;
    112  1.46       mrg #ifdef NOT_DEBUG
    113  1.42        ad 	for (tp = LIST_FIRST(&uvm.page_free[
    114  1.20  drochner 		free_list].pgfl_buckets[color].pgfl_queues[pgflidx]);
    115  1.20  drochner 	     tp != NULL;
    116  1.42        ad 	     tp = LIST_NEXT(tp, pageq.list)) {
    117  1.20  drochner 		if (tp == pg)
    118  1.20  drochner 			break;
    119  1.20  drochner 	}
    120  1.20  drochner 	if (tp == NULL)
    121  1.20  drochner 		panic("uvm_pglistalloc: page not on freelist");
    122  1.20  drochner #endif
    123  1.42        ad 	LIST_REMOVE(pg, pageq.list);	/* global */
    124  1.42        ad 	LIST_REMOVE(pg, listq.list);	/* cpu */
    125  1.20  drochner 	uvmexp.free--;
    126  1.20  drochner 	if (pg->flags & PG_ZERO)
    127  1.20  drochner 		uvmexp.zeropages--;
    128  1.42        ad 	VM_FREE_PAGE_TO_CPU(pg)->pages[pgflidx]--;
    129  1.20  drochner 	pg->flags = PG_CLEAN;
    130  1.20  drochner 	pg->pqflags = 0;
    131  1.20  drochner 	pg->uobject = NULL;
    132  1.20  drochner 	pg->uanon = NULL;
    133  1.42        ad 	TAILQ_INSERT_TAIL(rlist, pg, pageq.queue);
    134  1.20  drochner 	STAT_INCR(uvm_pglistalloc_npages);
    135  1.20  drochner }
    136  1.20  drochner 
    137  1.20  drochner static int
    138  1.33   thorpej uvm_pglistalloc_c_ps(struct vm_physseg *ps, int num, paddr_t low, paddr_t high,
    139  1.33   thorpej     paddr_t alignment, paddr_t boundary, struct pglist *rlist)
    140   1.1       mrg {
    141  1.22  drochner 	int try, limit, tryidx, end, idx;
    142  1.20  drochner 	struct vm_page *pgs;
    143  1.24  drochner 	int pagemask;
    144  1.24  drochner #ifdef DEBUG
    145  1.22  drochner 	paddr_t idxpa, lastidxpa;
    146  1.35  christos 	int cidx = 0;	/* XXX: GCC */
    147  1.22  drochner #endif
    148  1.24  drochner #ifdef PGALLOC_VERBOSE
    149  1.26   thorpej 	printf("pgalloc: contig %d pgs from psi %ld\n", num,
    150  1.47  uebayasi 	(long)(ps - VM_PHYSMEM_PTR(0)));
    151  1.24  drochner #endif
    152   1.1       mrg 
    153  1.39        ad 	KASSERT(mutex_owned(&uvm_fpageqlock));
    154  1.39        ad 
    155  1.24  drochner 	try = roundup(max(atop(low), ps->avail_start), atop(alignment));
    156  1.24  drochner 	limit = min(atop(high), ps->avail_end);
    157  1.24  drochner 	pagemask = ~((boundary >> PAGE_SHIFT) - 1);
    158  1.12       chs 
    159  1.24  drochner 	for (;;) {
    160  1.24  drochner 		if (try + num > limit) {
    161   1.3       mrg 			/*
    162   1.3       mrg 			 * We've run past the allowable range.
    163   1.3       mrg 			 */
    164  1.22  drochner 			return (0); /* FAIL */
    165   1.3       mrg 		}
    166  1.24  drochner 		if (boundary != 0 &&
    167  1.24  drochner 		    ((try ^ (try + num - 1)) & pagemask) != 0) {
    168  1.24  drochner 			/*
    169  1.24  drochner 			 * Region crosses boundary. Jump to the boundary
    170  1.24  drochner 			 * just crossed and ensure alignment.
    171  1.24  drochner 			 */
    172  1.24  drochner 			try = (try + num - 1) & pagemask;
    173  1.24  drochner 			try = roundup(try, atop(alignment));
    174  1.24  drochner 			continue;
    175  1.24  drochner 		}
    176  1.22  drochner #ifdef DEBUG
    177   1.3       mrg 		/*
    178   1.3       mrg 		 * Make sure this is a managed physical page.
    179   1.3       mrg 		 */
    180   1.3       mrg 
    181  1.49  uebayasi 		if (vm_physseg_find(try, &cidx) != vm_physmem_index(ps))
    182  1.22  drochner 			panic("pgalloc contig: botch1");
    183  1.24  drochner 		if (cidx != try - ps->start)
    184  1.22  drochner 			panic("pgalloc contig: botch2");
    185  1.49  uebayasi 		if (vm_physseg_find(try + num - 1, &cidx) != vm_physmem_index(ps))
    186  1.22  drochner 			panic("pgalloc contig: botch3");
    187  1.24  drochner 		if (cidx != try - ps->start + num - 1)
    188  1.31  junyoung 			panic("pgalloc contig: botch4");
    189  1.22  drochner #endif
    190  1.24  drochner 		tryidx = try - ps->start;
    191  1.24  drochner 		end = tryidx + num;
    192  1.24  drochner 		pgs = ps->pgs;
    193   1.3       mrg 
    194   1.3       mrg 		/*
    195  1.24  drochner 		 * Found a suitable starting page.  See if the range is free.
    196   1.3       mrg 		 */
    197  1.22  drochner 		for (idx = tryidx; idx < end; idx++) {
    198  1.24  drochner 			if (VM_PAGE_IS_FREE(&pgs[idx]) == 0)
    199   1.3       mrg 				break;
    200  1.24  drochner 
    201  1.24  drochner #ifdef DEBUG
    202   1.3       mrg 			idxpa = VM_PAGE_TO_PHYS(&pgs[idx]);
    203   1.3       mrg 			if (idx > tryidx) {
    204   1.3       mrg 				lastidxpa = VM_PAGE_TO_PHYS(&pgs[idx - 1]);
    205  1.12       chs 				if ((lastidxpa + PAGE_SIZE) != idxpa) {
    206   1.3       mrg 					/*
    207   1.3       mrg 					 * Region not contiguous.
    208   1.3       mrg 					 */
    209  1.22  drochner 					panic("pgalloc contig: botch5");
    210   1.3       mrg 				}
    211   1.3       mrg 				if (boundary != 0 &&
    212  1.24  drochner 				    ((lastidxpa ^ idxpa) & ~(boundary - 1))
    213  1.24  drochner 				    != 0) {
    214   1.3       mrg 					/*
    215   1.3       mrg 					 * Region crosses boundary.
    216   1.3       mrg 					 */
    217  1.24  drochner 					panic("pgalloc contig: botch6");
    218   1.3       mrg 				}
    219   1.3       mrg 			}
    220  1.24  drochner #endif
    221   1.3       mrg 		}
    222  1.24  drochner 		if (idx == end)
    223   1.3       mrg 			break;
    224  1.24  drochner 
    225  1.24  drochner 		try += atop(alignment);
    226   1.1       mrg 	}
    227   1.1       mrg 
    228   1.3       mrg 	/*
    229   1.3       mrg 	 * we have a chunk of memory that conforms to the requested constraints.
    230   1.3       mrg 	 */
    231   1.3       mrg 	idx = tryidx;
    232  1.24  drochner 	while (idx < end)
    233  1.20  drochner 		uvm_pglist_add(&pgs[idx++], rlist);
    234  1.24  drochner 
    235  1.24  drochner #ifdef PGALLOC_VERBOSE
    236  1.24  drochner 	printf("got %d pgs\n", num);
    237  1.24  drochner #endif
    238  1.24  drochner 	return (num); /* number of pages allocated */
    239  1.22  drochner }
    240  1.22  drochner 
    241  1.22  drochner static int
    242  1.33   thorpej uvm_pglistalloc_contig(int num, paddr_t low, paddr_t high, paddr_t alignment,
    243  1.33   thorpej     paddr_t boundary, struct pglist *rlist)
    244  1.22  drochner {
    245  1.22  drochner 	int fl, psi;
    246  1.24  drochner 	struct vm_physseg *ps;
    247  1.38        ad 	int error;
    248  1.22  drochner 
    249  1.22  drochner 	/* Default to "lose". */
    250  1.22  drochner 	error = ENOMEM;
    251  1.22  drochner 
    252  1.22  drochner 	/*
    253  1.22  drochner 	 * Block all memory allocation and lock the free list.
    254  1.22  drochner 	 */
    255  1.38        ad 	mutex_spin_enter(&uvm_fpageqlock);
    256  1.22  drochner 
    257  1.22  drochner 	/* Are there even any free pages? */
    258  1.22  drochner 	if (uvmexp.free <= (uvmexp.reserve_pagedaemon + uvmexp.reserve_kernel))
    259  1.22  drochner 		goto out;
    260  1.22  drochner 
    261  1.22  drochner 	for (fl = 0; fl < VM_NFREELIST; fl++) {
    262  1.22  drochner #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
    263  1.47  uebayasi 		for (psi = vm_nphysmem - 1 ; psi >= 0 ; psi--)
    264  1.22  drochner #else
    265  1.47  uebayasi 		for (psi = 0 ; psi < vm_nphysmem ; psi++)
    266  1.22  drochner #endif
    267  1.22  drochner 		{
    268  1.47  uebayasi 			ps = VM_PHYSMEM_PTR(psi);
    269  1.24  drochner 
    270  1.24  drochner 			if (ps->free_list != fl)
    271  1.22  drochner 				continue;
    272  1.22  drochner 
    273  1.24  drochner 			num -= uvm_pglistalloc_c_ps(ps, num, low, high,
    274  1.24  drochner 						    alignment, boundary, rlist);
    275  1.24  drochner 			if (num == 0) {
    276  1.24  drochner #ifdef PGALLOC_VERBOSE
    277  1.44   reinoud 				printf("pgalloc: %"PRIxMAX"-%"PRIxMAX"\n",
    278  1.44   reinoud 				       (uintmax_t) VM_PAGE_TO_PHYS(TAILQ_FIRST(rlist)),
    279  1.44   reinoud 				       (uintmax_t) VM_PAGE_TO_PHYS(TAILQ_LAST(rlist, pglist)));
    280  1.22  drochner #endif
    281  1.22  drochner 				error = 0;
    282  1.22  drochner 				goto out;
    283  1.22  drochner 			}
    284  1.22  drochner 		}
    285  1.22  drochner 	}
    286  1.20  drochner 
    287  1.20  drochner out:
    288  1.20  drochner 	/*
    289  1.20  drochner 	 * check to see if we need to generate some free pages waking
    290  1.20  drochner 	 * the pagedaemon.
    291  1.20  drochner 	 */
    292  1.20  drochner 
    293  1.36      yamt 	uvm_kick_pdaemon();
    294  1.38        ad 	mutex_spin_exit(&uvm_fpageqlock);
    295  1.20  drochner 	return (error);
    296  1.20  drochner }
    297  1.20  drochner 
    298  1.24  drochner static int
    299  1.33   thorpej uvm_pglistalloc_s_ps(struct vm_physseg *ps, int num, paddr_t low, paddr_t high,
    300  1.33   thorpej     struct pglist *rlist)
    301  1.22  drochner {
    302  1.24  drochner 	int todo, limit, try;
    303  1.22  drochner 	struct vm_page *pg;
    304  1.22  drochner #ifdef DEBUG
    305  1.35  christos 	int cidx = 0;	/* XXX: GCC */
    306  1.22  drochner #endif
    307  1.24  drochner #ifdef PGALLOC_VERBOSE
    308  1.26   thorpej 	printf("pgalloc: simple %d pgs from psi %ld\n", num,
    309  1.47  uebayasi 	    (long)(ps - VM_PHYSMEM_PTR(0)));
    310  1.24  drochner #endif
    311  1.22  drochner 
    312  1.39        ad 	KASSERT(mutex_owned(&uvm_fpageqlock));
    313  1.39        ad 
    314  1.24  drochner 	todo = num;
    315  1.24  drochner 	limit = min(atop(high), ps->avail_end);
    316  1.22  drochner 
    317  1.24  drochner 	for (try = max(atop(low), ps->avail_start);
    318  1.22  drochner 	     try < limit; try ++) {
    319  1.22  drochner #ifdef DEBUG
    320  1.49  uebayasi 		if (vm_physseg_find(try, &cidx) != vm_physmem_index(ps))
    321  1.22  drochner 			panic("pgalloc simple: botch1");
    322  1.24  drochner 		if (cidx != (try - ps->start))
    323  1.22  drochner 			panic("pgalloc simple: botch2");
    324  1.22  drochner #endif
    325  1.24  drochner 		pg = &ps->pgs[try - ps->start];
    326  1.22  drochner 		if (VM_PAGE_IS_FREE(pg) == 0)
    327  1.22  drochner 			continue;
    328  1.22  drochner 
    329  1.22  drochner 		uvm_pglist_add(pg, rlist);
    330  1.24  drochner 		if (--todo == 0)
    331  1.22  drochner 			break;
    332  1.22  drochner 	}
    333  1.24  drochner 
    334  1.24  drochner #ifdef PGALLOC_VERBOSE
    335  1.24  drochner 	printf("got %d pgs\n", num - todo);
    336  1.24  drochner #endif
    337  1.24  drochner 	return (num - todo); /* number of pages allocated */
    338  1.22  drochner }
    339  1.22  drochner 
    340  1.20  drochner static int
    341  1.33   thorpej uvm_pglistalloc_simple(int num, paddr_t low, paddr_t high,
    342  1.33   thorpej     struct pglist *rlist, int waitok)
    343  1.20  drochner {
    344  1.38        ad 	int fl, psi, error;
    345  1.24  drochner 	struct vm_physseg *ps;
    346  1.20  drochner 
    347  1.20  drochner 	/* Default to "lose". */
    348  1.20  drochner 	error = ENOMEM;
    349  1.20  drochner 
    350  1.20  drochner again:
    351  1.20  drochner 	/*
    352  1.20  drochner 	 * Block all memory allocation and lock the free list.
    353  1.20  drochner 	 */
    354  1.38        ad 	mutex_spin_enter(&uvm_fpageqlock);
    355  1.20  drochner 
    356  1.20  drochner 	/* Are there even any free pages? */
    357  1.20  drochner 	if (uvmexp.free <= (uvmexp.reserve_pagedaemon + uvmexp.reserve_kernel))
    358  1.20  drochner 		goto out;
    359  1.20  drochner 
    360  1.22  drochner 	for (fl = 0; fl < VM_NFREELIST; fl++) {
    361  1.22  drochner #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
    362  1.47  uebayasi 		for (psi = vm_nphysmem - 1 ; psi >= 0 ; psi--)
    363  1.22  drochner #else
    364  1.47  uebayasi 		for (psi = 0 ; psi < vm_nphysmem ; psi++)
    365  1.22  drochner #endif
    366  1.22  drochner 		{
    367  1.47  uebayasi 			ps = VM_PHYSMEM_PTR(psi);
    368  1.24  drochner 
    369  1.24  drochner 			if (ps->free_list != fl)
    370  1.22  drochner 				continue;
    371  1.22  drochner 
    372  1.24  drochner 			num -= uvm_pglistalloc_s_ps(ps, num, low, high, rlist);
    373  1.24  drochner 			if (num == 0) {
    374  1.22  drochner 				error = 0;
    375  1.22  drochner 				goto out;
    376  1.22  drochner 			}
    377  1.22  drochner 		}
    378  1.20  drochner 
    379   1.3       mrg 	}
    380   1.1       mrg 
    381   1.1       mrg out:
    382   1.3       mrg 	/*
    383   1.3       mrg 	 * check to see if we need to generate some free pages waking
    384   1.3       mrg 	 * the pagedaemon.
    385   1.3       mrg 	 */
    386  1.15       chs 
    387  1.36      yamt 	uvm_kick_pdaemon();
    388  1.38        ad 	mutex_spin_exit(&uvm_fpageqlock);
    389  1.38        ad 
    390  1.20  drochner 	if (error) {
    391  1.20  drochner 		if (waitok) {
    392  1.20  drochner 			/* XXX perhaps some time limitation? */
    393  1.20  drochner #ifdef DEBUG
    394  1.20  drochner 			printf("pglistalloc waiting\n");
    395  1.20  drochner #endif
    396  1.20  drochner 			uvm_wait("pglalloc");
    397  1.20  drochner 			goto again;
    398  1.20  drochner 		} else
    399  1.20  drochner 			uvm_pglistfree(rlist);
    400  1.20  drochner 	}
    401  1.24  drochner #ifdef PGALLOC_VERBOSE
    402  1.22  drochner 	if (!error)
    403  1.44   reinoud 		printf("pgalloc: %"PRIxMAX"..%"PRIxMAX"\n",
    404  1.44   reinoud 		       (uintmax_t) VM_PAGE_TO_PHYS(TAILQ_FIRST(rlist)),
    405  1.44   reinoud 		       (uintmax_t) VM_PAGE_TO_PHYS(TAILQ_LAST(rlist, pglist)));
    406  1.22  drochner #endif
    407   1.3       mrg 	return (error);
    408  1.20  drochner }
    409  1.20  drochner 
    410  1.20  drochner int
    411  1.33   thorpej uvm_pglistalloc(psize_t size, paddr_t low, paddr_t high, paddr_t alignment,
    412  1.33   thorpej     paddr_t boundary, struct pglist *rlist, int nsegs, int waitok)
    413  1.20  drochner {
    414  1.24  drochner 	int num, res;
    415  1.20  drochner 
    416  1.20  drochner 	KASSERT((alignment & (alignment - 1)) == 0);
    417  1.20  drochner 	KASSERT((boundary & (boundary - 1)) == 0);
    418  1.20  drochner 
    419  1.20  drochner 	/*
    420  1.20  drochner 	 * Our allocations are always page granularity, so our alignment
    421  1.20  drochner 	 * must be, too.
    422  1.20  drochner 	 */
    423  1.20  drochner 	if (alignment < PAGE_SIZE)
    424  1.20  drochner 		alignment = PAGE_SIZE;
    425  1.24  drochner 	if (boundary != 0 && boundary < size)
    426  1.24  drochner 		return (EINVAL);
    427  1.24  drochner 	num = atop(round_page(size));
    428  1.20  drochner 	low = roundup(low, alignment);
    429  1.21  drochner 
    430  1.21  drochner 	TAILQ_INIT(rlist);
    431  1.20  drochner 
    432  1.23     enami 	if ((nsegs < size >> PAGE_SHIFT) || (alignment != PAGE_SIZE) ||
    433  1.23     enami 	    (boundary != 0))
    434  1.24  drochner 		res = uvm_pglistalloc_contig(num, low, high, alignment,
    435  1.24  drochner 					     boundary, rlist);
    436  1.20  drochner 	else
    437  1.24  drochner 		res = uvm_pglistalloc_simple(num, low, high, rlist, waitok);
    438  1.20  drochner 
    439  1.20  drochner 	return (res);
    440   1.1       mrg }
    441   1.1       mrg 
    442   1.1       mrg /*
    443   1.1       mrg  * uvm_pglistfree: free a list of pages
    444   1.1       mrg  *
    445   1.1       mrg  * => pages should already be unmapped
    446   1.1       mrg  */
    447   1.1       mrg 
    448   1.3       mrg void
    449  1.33   thorpej uvm_pglistfree(struct pglist *list)
    450   1.1       mrg {
    451  1.42        ad 	struct uvm_cpu *ucpu;
    452  1.18       chs 	struct vm_page *pg;
    453  1.42        ad 	int index, color, queue;
    454  1.42        ad 	bool iszero;
    455   1.1       mrg 
    456   1.3       mrg 	/*
    457  1.18       chs 	 * Lock the free list and free each page.
    458   1.3       mrg 	 */
    459  1.18       chs 
    460  1.38        ad 	mutex_spin_enter(&uvm_fpageqlock);
    461  1.42        ad 	ucpu = curcpu()->ci_data.cpu_uvm;
    462  1.18       chs 	while ((pg = TAILQ_FIRST(list)) != NULL) {
    463  1.36      yamt 		KASSERT(!uvmpdpol_pageisqueued_p(pg));
    464  1.42        ad 		TAILQ_REMOVE(list, pg, pageq.queue);
    465  1.29      yamt 		iszero = (pg->flags & PG_ZERO);
    466  1.18       chs 		pg->pqflags = PQ_FREE;
    467  1.29      yamt #ifdef DEBUG
    468  1.29      yamt 		pg->uobject = (void *)0xdeadbeef;
    469  1.29      yamt 		pg->uanon = (void *)0xdeadbeef;
    470  1.30      yamt #endif /* DEBUG */
    471  1.30      yamt #ifdef DEBUG
    472  1.30      yamt 		if (iszero)
    473  1.30      yamt 			uvm_pagezerocheck(pg);
    474  1.30      yamt #endif /* DEBUG */
    475  1.42        ad 		index = uvm_page_lookup_freelist(pg);
    476  1.42        ad 		color = VM_PGCOLOR_BUCKET(pg);
    477  1.42        ad 		queue = iszero ? PGFL_ZEROS : PGFL_UNKNOWN;
    478  1.42        ad 		pg->offset = (uintptr_t)ucpu;
    479  1.42        ad 		LIST_INSERT_HEAD(&uvm.page_free[index].pgfl_buckets[color].
    480  1.42        ad 		    pgfl_queues[queue], pg, pageq.list);
    481  1.42        ad 		LIST_INSERT_HEAD(&ucpu->page_free[index].pgfl_buckets[color].
    482  1.42        ad 		    pgfl_queues[queue], pg, listq.list);
    483   1.3       mrg 		uvmexp.free++;
    484  1.29      yamt 		if (iszero)
    485  1.29      yamt 			uvmexp.zeropages++;
    486  1.42        ad 		ucpu->pages[queue]++;
    487   1.3       mrg 		STAT_DECR(uvm_pglistalloc_npages);
    488   1.3       mrg 	}
    489  1.42        ad 	if (ucpu->pages[PGFL_ZEROS] < ucpu->pages[PGFL_UNKNOWN])
    490  1.42        ad 		ucpu->page_idle_zero = vm_page_zero_enable;
    491  1.38        ad 	mutex_spin_exit(&uvm_fpageqlock);
    492   1.1       mrg }
    493