Home | History | Annotate | Line # | Download | only in uvm
uvm_pglist.c revision 1.17.2.3
      1  1.17.2.3  jdolecek /*	$NetBSD: uvm_pglist.c,v 1.17.2.3 2002/09/06 08:50:26 jdolecek 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.1       mrg 
     44  1.17.2.1   thorpej #include <sys/cdefs.h>
     45  1.17.2.3  jdolecek __KERNEL_RCSID(0, "$NetBSD: uvm_pglist.c,v 1.17.2.3 2002/09/06 08:50:26 jdolecek Exp $");
     46  1.17.2.1   thorpej 
     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.1       mrg 	} while (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.1       mrg  * => allocated pages are placed at the tail of rlist.  rlist is
     72       1.1       mrg  *    assumed to be properly initialized by caller.
     73       1.1       mrg  * => returns 0 on success or errno on failure
     74       1.1       mrg  * => XXX: implementation allocates only a single segment, also
     75       1.1       mrg  *	might be able to better advantage of vm_physeg[].
     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.17.2.2  jdolecek static void uvm_pglist_add(struct vm_page *, struct pglist *);
     88  1.17.2.3  jdolecek static int uvm_pglistalloc_c_ps(struct vm_physseg *, int, paddr_t, paddr_t,
     89  1.17.2.3  jdolecek 				paddr_t, paddr_t, struct pglist *);
     90  1.17.2.3  jdolecek static int uvm_pglistalloc_contig(int, paddr_t, paddr_t, paddr_t, paddr_t,
     91  1.17.2.2  jdolecek 				  struct pglist *);
     92  1.17.2.3  jdolecek static int uvm_pglistalloc_s_ps(struct vm_physseg *, int, paddr_t, paddr_t,
     93  1.17.2.3  jdolecek 				struct pglist *);
     94  1.17.2.3  jdolecek static int uvm_pglistalloc_simple(int, paddr_t, paddr_t,
     95  1.17.2.2  jdolecek 				  struct pglist *, int);
     96  1.17.2.2  jdolecek 
     97  1.17.2.2  jdolecek static void
     98  1.17.2.2  jdolecek uvm_pglist_add(pg, rlist)
     99  1.17.2.2  jdolecek 	struct vm_page *pg;
    100       1.3       mrg 	struct pglist *rlist;
    101       1.1       mrg {
    102  1.17.2.2  jdolecek 	int free_list, color, pgflidx;
    103       1.1       mrg #ifdef DEBUG
    104      1.16       chs 	struct vm_page *tp;
    105       1.1       mrg #endif
    106       1.1       mrg 
    107  1.17.2.2  jdolecek #if PGFL_NQUEUES != 2
    108  1.17.2.2  jdolecek #error uvm_pglistalloc needs to be updated
    109  1.17.2.2  jdolecek #endif
    110      1.15       chs 
    111  1.17.2.2  jdolecek 	free_list = uvm_page_lookup_freelist(pg);
    112  1.17.2.2  jdolecek 	color = VM_PGCOLOR_BUCKET(pg);
    113  1.17.2.2  jdolecek 	pgflidx = (pg->flags & PG_ZERO) ? PGFL_ZEROS : PGFL_UNKNOWN;
    114  1.17.2.2  jdolecek #ifdef DEBUG
    115  1.17.2.2  jdolecek 	for (tp = TAILQ_FIRST(&uvm.page_free[
    116  1.17.2.2  jdolecek 		free_list].pgfl_buckets[color].pgfl_queues[pgflidx]);
    117  1.17.2.2  jdolecek 	     tp != NULL;
    118  1.17.2.2  jdolecek 	     tp = TAILQ_NEXT(tp, pageq)) {
    119  1.17.2.2  jdolecek 		if (tp == pg)
    120  1.17.2.2  jdolecek 			break;
    121  1.17.2.2  jdolecek 	}
    122  1.17.2.2  jdolecek 	if (tp == NULL)
    123  1.17.2.2  jdolecek 		panic("uvm_pglistalloc: page not on freelist");
    124  1.17.2.2  jdolecek #endif
    125  1.17.2.2  jdolecek 	TAILQ_REMOVE(&uvm.page_free[free_list].pgfl_buckets[
    126  1.17.2.2  jdolecek 			color].pgfl_queues[pgflidx], pg, pageq);
    127  1.17.2.2  jdolecek 	uvmexp.free--;
    128  1.17.2.2  jdolecek 	if (pg->flags & PG_ZERO)
    129  1.17.2.2  jdolecek 		uvmexp.zeropages--;
    130  1.17.2.2  jdolecek 	pg->flags = PG_CLEAN;
    131  1.17.2.2  jdolecek 	pg->pqflags = 0;
    132  1.17.2.2  jdolecek 	pg->uobject = NULL;
    133  1.17.2.2  jdolecek 	pg->uanon = NULL;
    134  1.17.2.2  jdolecek 	TAILQ_INSERT_TAIL(rlist, pg, pageq);
    135  1.17.2.2  jdolecek 	STAT_INCR(uvm_pglistalloc_npages);
    136  1.17.2.2  jdolecek }
    137  1.17.2.2  jdolecek 
    138  1.17.2.2  jdolecek static int
    139  1.17.2.3  jdolecek uvm_pglistalloc_c_ps(ps, num, low, high, alignment, boundary, rlist)
    140  1.17.2.3  jdolecek 	struct vm_physseg *ps;
    141  1.17.2.3  jdolecek 	int num;
    142  1.17.2.2  jdolecek 	paddr_t low, high, alignment, boundary;
    143  1.17.2.2  jdolecek 	struct pglist *rlist;
    144  1.17.2.2  jdolecek {
    145  1.17.2.3  jdolecek 	int try, limit, tryidx, end, idx;
    146  1.17.2.2  jdolecek 	struct vm_page *pgs;
    147  1.17.2.3  jdolecek 	int pagemask;
    148  1.17.2.3  jdolecek #ifdef DEBUG
    149  1.17.2.3  jdolecek 	paddr_t idxpa, lastidxpa;
    150  1.17.2.3  jdolecek 	int cidx;
    151  1.17.2.3  jdolecek #endif
    152  1.17.2.3  jdolecek #ifdef PGALLOC_VERBOSE
    153  1.17.2.3  jdolecek 	printf("pgalloc: contig %d pgs from psi %d\n", num, ps - vm_physmem);
    154  1.17.2.3  jdolecek #endif
    155       1.3       mrg 
    156  1.17.2.3  jdolecek 	try = roundup(max(atop(low), ps->avail_start), atop(alignment));
    157  1.17.2.3  jdolecek 	limit = min(atop(high), ps->avail_end);
    158  1.17.2.3  jdolecek 	pagemask = ~((boundary >> PAGE_SHIFT) - 1);
    159      1.12       chs 
    160  1.17.2.3  jdolecek 	for (;;) {
    161  1.17.2.3  jdolecek 		if (try + num > limit) {
    162       1.3       mrg 			/*
    163       1.3       mrg 			 * We've run past the allowable range.
    164       1.3       mrg 			 */
    165  1.17.2.3  jdolecek 			return (0); /* FAIL */
    166       1.3       mrg 		}
    167  1.17.2.3  jdolecek 		if (boundary != 0 &&
    168  1.17.2.3  jdolecek 		    ((try ^ (try + num - 1)) & pagemask) != 0) {
    169  1.17.2.3  jdolecek 			/*
    170  1.17.2.3  jdolecek 			 * Region crosses boundary. Jump to the boundary
    171  1.17.2.3  jdolecek 			 * just crossed and ensure alignment.
    172  1.17.2.3  jdolecek 			 */
    173  1.17.2.3  jdolecek 			try = (try + num - 1) & pagemask;
    174  1.17.2.3  jdolecek 			try = roundup(try, atop(alignment));
    175  1.17.2.3  jdolecek 			continue;
    176  1.17.2.3  jdolecek 		}
    177  1.17.2.3  jdolecek #ifdef DEBUG
    178       1.3       mrg 		/*
    179       1.3       mrg 		 * Make sure this is a managed physical page.
    180       1.3       mrg 		 */
    181       1.3       mrg 
    182  1.17.2.3  jdolecek 		if (vm_physseg_find(try, &cidx) != ps - vm_physmem)
    183  1.17.2.3  jdolecek 			panic("pgalloc contig: botch1");
    184  1.17.2.3  jdolecek 		if (cidx != try - ps->start)
    185  1.17.2.3  jdolecek 			panic("pgalloc contig: botch2");
    186  1.17.2.3  jdolecek 		if (vm_physseg_find(try + num - 1, &cidx) != ps - vm_physmem)
    187  1.17.2.3  jdolecek 			panic("pgalloc contig: botch3");
    188  1.17.2.3  jdolecek 		if (cidx != try - ps->start + num - 1)
    189  1.17.2.3  jdolecek 			panic("pgalloc contig: botch4");
    190  1.17.2.3  jdolecek #endif
    191  1.17.2.3  jdolecek 		tryidx = try - ps->start;
    192  1.17.2.3  jdolecek 		end = tryidx + num;
    193  1.17.2.3  jdolecek 		pgs = ps->pgs;
    194       1.3       mrg 
    195       1.3       mrg 		/*
    196  1.17.2.3  jdolecek 		 * Found a suitable starting page.  See if the range is free.
    197       1.3       mrg 		 */
    198  1.17.2.3  jdolecek 		for (idx = tryidx; idx < end; idx++) {
    199  1.17.2.3  jdolecek 			if (VM_PAGE_IS_FREE(&pgs[idx]) == 0)
    200       1.3       mrg 				break;
    201  1.17.2.3  jdolecek 
    202  1.17.2.3  jdolecek #ifdef DEBUG
    203       1.3       mrg 			idxpa = VM_PAGE_TO_PHYS(&pgs[idx]);
    204       1.3       mrg 			if (idx > tryidx) {
    205       1.3       mrg 				lastidxpa = VM_PAGE_TO_PHYS(&pgs[idx - 1]);
    206      1.12       chs 				if ((lastidxpa + PAGE_SIZE) != idxpa) {
    207       1.3       mrg 					/*
    208       1.3       mrg 					 * Region not contiguous.
    209       1.3       mrg 					 */
    210  1.17.2.3  jdolecek 					panic("pgalloc contig: botch5");
    211       1.3       mrg 				}
    212       1.3       mrg 				if (boundary != 0 &&
    213  1.17.2.3  jdolecek 				    ((lastidxpa ^ idxpa) & ~(boundary - 1))
    214  1.17.2.3  jdolecek 				    != 0) {
    215       1.3       mrg 					/*
    216       1.3       mrg 					 * Region crosses boundary.
    217       1.3       mrg 					 */
    218  1.17.2.3  jdolecek 					panic("pgalloc contig: botch6");
    219       1.3       mrg 				}
    220       1.3       mrg 			}
    221  1.17.2.3  jdolecek #endif
    222       1.3       mrg 		}
    223  1.17.2.3  jdolecek 		if (idx == end)
    224       1.3       mrg 			break;
    225  1.17.2.3  jdolecek 
    226  1.17.2.3  jdolecek 		try += atop(alignment);
    227       1.1       mrg 	}
    228       1.1       mrg 
    229       1.3       mrg 	/*
    230       1.3       mrg 	 * we have a chunk of memory that conforms to the requested constraints.
    231       1.3       mrg 	 */
    232       1.3       mrg 	idx = tryidx;
    233  1.17.2.3  jdolecek 	while (idx < end)
    234  1.17.2.2  jdolecek 		uvm_pglist_add(&pgs[idx++], rlist);
    235  1.17.2.3  jdolecek 
    236  1.17.2.3  jdolecek #ifdef PGALLOC_VERBOSE
    237  1.17.2.3  jdolecek 	printf("got %d pgs\n", num);
    238  1.17.2.3  jdolecek #endif
    239  1.17.2.3  jdolecek 	return (num); /* number of pages allocated */
    240  1.17.2.3  jdolecek }
    241  1.17.2.3  jdolecek 
    242  1.17.2.3  jdolecek static int
    243  1.17.2.3  jdolecek uvm_pglistalloc_contig(num, low, high, alignment, boundary, rlist)
    244  1.17.2.3  jdolecek 	int num;
    245  1.17.2.3  jdolecek 	paddr_t low, high, alignment, boundary;
    246  1.17.2.3  jdolecek 	struct pglist *rlist;
    247  1.17.2.3  jdolecek {
    248  1.17.2.3  jdolecek 	int fl, psi;
    249  1.17.2.3  jdolecek 	struct vm_physseg *ps;
    250  1.17.2.3  jdolecek 	int s, error;
    251  1.17.2.3  jdolecek 
    252  1.17.2.3  jdolecek 	/* Default to "lose". */
    253  1.17.2.3  jdolecek 	error = ENOMEM;
    254  1.17.2.3  jdolecek 
    255  1.17.2.3  jdolecek 	/*
    256  1.17.2.3  jdolecek 	 * Block all memory allocation and lock the free list.
    257  1.17.2.3  jdolecek 	 */
    258  1.17.2.3  jdolecek 	s = uvm_lock_fpageq();
    259  1.17.2.3  jdolecek 
    260  1.17.2.3  jdolecek 	/* Are there even any free pages? */
    261  1.17.2.3  jdolecek 	if (uvmexp.free <= (uvmexp.reserve_pagedaemon + uvmexp.reserve_kernel))
    262  1.17.2.3  jdolecek 		goto out;
    263  1.17.2.3  jdolecek 
    264  1.17.2.3  jdolecek 	for (fl = 0; fl < VM_NFREELIST; fl++) {
    265  1.17.2.3  jdolecek #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
    266  1.17.2.3  jdolecek 		for (psi = vm_nphysseg - 1 ; psi >= 0 ; psi--)
    267  1.17.2.3  jdolecek #else
    268  1.17.2.3  jdolecek 		for (psi = 0 ; psi < vm_nphysseg ; psi++)
    269  1.17.2.3  jdolecek #endif
    270  1.17.2.3  jdolecek 		{
    271  1.17.2.3  jdolecek 			ps = &vm_physmem[psi];
    272  1.17.2.3  jdolecek 
    273  1.17.2.3  jdolecek 			if (ps->free_list != fl)
    274  1.17.2.3  jdolecek 				continue;
    275  1.17.2.3  jdolecek 
    276  1.17.2.3  jdolecek 			num -= uvm_pglistalloc_c_ps(ps, num, low, high,
    277  1.17.2.3  jdolecek 						    alignment, boundary, rlist);
    278  1.17.2.3  jdolecek 			if (num == 0) {
    279  1.17.2.3  jdolecek #ifdef PGALLOC_VERBOSE
    280  1.17.2.3  jdolecek 				printf("pgalloc: %lx-%lx\n",
    281  1.17.2.3  jdolecek 				       TAILQ_FIRST(rlist)->phys_addr,
    282  1.17.2.3  jdolecek 				       TAILQ_LAST(rlist, pglist)->phys_addr);
    283  1.17.2.3  jdolecek #endif
    284  1.17.2.3  jdolecek 				error = 0;
    285  1.17.2.3  jdolecek 				goto out;
    286  1.17.2.3  jdolecek 			}
    287  1.17.2.3  jdolecek 		}
    288       1.3       mrg 	}
    289       1.1       mrg 
    290       1.1       mrg out:
    291       1.3       mrg 	/*
    292       1.3       mrg 	 * check to see if we need to generate some free pages waking
    293       1.3       mrg 	 * the pagedaemon.
    294       1.3       mrg 	 */
    295      1.15       chs 
    296      1.17   thorpej 	UVM_KICK_PDAEMON();
    297      1.12       chs 	uvm_unlock_fpageq(s);
    298       1.3       mrg 	return (error);
    299  1.17.2.2  jdolecek }
    300  1.17.2.2  jdolecek 
    301  1.17.2.2  jdolecek static int
    302  1.17.2.3  jdolecek uvm_pglistalloc_s_ps(ps, num, low, high, rlist)
    303  1.17.2.3  jdolecek 	struct vm_physseg *ps;
    304  1.17.2.3  jdolecek 	int num;
    305  1.17.2.2  jdolecek 	paddr_t low, high;
    306  1.17.2.2  jdolecek 	struct pglist *rlist;
    307  1.17.2.2  jdolecek {
    308  1.17.2.3  jdolecek 	int todo, limit, try;
    309  1.17.2.2  jdolecek 	struct vm_page *pg;
    310  1.17.2.3  jdolecek #ifdef DEBUG
    311  1.17.2.3  jdolecek 	int cidx;
    312  1.17.2.3  jdolecek #endif
    313  1.17.2.3  jdolecek #ifdef PGALLOC_VERBOSE
    314  1.17.2.3  jdolecek 	printf("pgalloc: simple %d pgs from psi %d\n", num, ps - vm_physmem);
    315  1.17.2.3  jdolecek #endif
    316  1.17.2.3  jdolecek 
    317  1.17.2.3  jdolecek 	todo = num;
    318  1.17.2.3  jdolecek 	limit = min(atop(high), ps->avail_end);
    319  1.17.2.3  jdolecek 
    320  1.17.2.3  jdolecek 	for (try = max(atop(low), ps->avail_start);
    321  1.17.2.3  jdolecek 	     try < limit; try ++) {
    322  1.17.2.3  jdolecek #ifdef DEBUG
    323  1.17.2.3  jdolecek 		if (vm_physseg_find(try, &cidx) != ps - vm_physmem)
    324  1.17.2.3  jdolecek 			panic("pgalloc simple: botch1");
    325  1.17.2.3  jdolecek 		if (cidx != (try - ps->start))
    326  1.17.2.3  jdolecek 			panic("pgalloc simple: botch2");
    327  1.17.2.3  jdolecek #endif
    328  1.17.2.3  jdolecek 		pg = &ps->pgs[try - ps->start];
    329  1.17.2.3  jdolecek 		if (VM_PAGE_IS_FREE(pg) == 0)
    330  1.17.2.3  jdolecek 			continue;
    331  1.17.2.3  jdolecek 
    332  1.17.2.3  jdolecek 		uvm_pglist_add(pg, rlist);
    333  1.17.2.3  jdolecek 		if (--todo == 0)
    334  1.17.2.3  jdolecek 			break;
    335  1.17.2.3  jdolecek 	}
    336  1.17.2.3  jdolecek 
    337  1.17.2.3  jdolecek #ifdef PGALLOC_VERBOSE
    338  1.17.2.3  jdolecek 	printf("got %d pgs\n", num - todo);
    339  1.17.2.3  jdolecek #endif
    340  1.17.2.3  jdolecek 	return (num - todo); /* number of pages allocated */
    341  1.17.2.3  jdolecek }
    342  1.17.2.3  jdolecek 
    343  1.17.2.3  jdolecek static int
    344  1.17.2.3  jdolecek uvm_pglistalloc_simple(num, low, high, rlist, waitok)
    345  1.17.2.3  jdolecek 	int num;
    346  1.17.2.3  jdolecek 	paddr_t low, high;
    347  1.17.2.3  jdolecek 	struct pglist *rlist;
    348  1.17.2.3  jdolecek 	int waitok;
    349  1.17.2.3  jdolecek {
    350  1.17.2.3  jdolecek 	int fl, psi, s, error;
    351  1.17.2.3  jdolecek 	struct vm_physseg *ps;
    352  1.17.2.2  jdolecek 
    353  1.17.2.2  jdolecek 	/* Default to "lose". */
    354  1.17.2.2  jdolecek 	error = ENOMEM;
    355  1.17.2.2  jdolecek 
    356  1.17.2.2  jdolecek again:
    357  1.17.2.2  jdolecek 	/*
    358  1.17.2.2  jdolecek 	 * Block all memory allocation and lock the free list.
    359  1.17.2.2  jdolecek 	 */
    360  1.17.2.2  jdolecek 	s = uvm_lock_fpageq();
    361  1.17.2.2  jdolecek 
    362  1.17.2.2  jdolecek 	/* Are there even any free pages? */
    363  1.17.2.2  jdolecek 	if (uvmexp.free <= (uvmexp.reserve_pagedaemon + uvmexp.reserve_kernel))
    364  1.17.2.2  jdolecek 		goto out;
    365  1.17.2.2  jdolecek 
    366  1.17.2.3  jdolecek 	for (fl = 0; fl < VM_NFREELIST; fl++) {
    367  1.17.2.3  jdolecek #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
    368  1.17.2.3  jdolecek 		for (psi = vm_nphysseg - 1 ; psi >= 0 ; psi--)
    369  1.17.2.3  jdolecek #else
    370  1.17.2.3  jdolecek 		for (psi = 0 ; psi < vm_nphysseg ; psi++)
    371  1.17.2.3  jdolecek #endif
    372  1.17.2.3  jdolecek 		{
    373  1.17.2.3  jdolecek 			ps = &vm_physmem[psi];
    374  1.17.2.2  jdolecek 
    375  1.17.2.3  jdolecek 			if (ps->free_list != fl)
    376  1.17.2.3  jdolecek 				continue;
    377  1.17.2.2  jdolecek 
    378  1.17.2.3  jdolecek 			num -= uvm_pglistalloc_s_ps(ps, num, low, high, rlist);
    379  1.17.2.3  jdolecek 			if (num == 0) {
    380  1.17.2.3  jdolecek 				error = 0;
    381  1.17.2.3  jdolecek 				goto out;
    382  1.17.2.3  jdolecek 			}
    383  1.17.2.2  jdolecek 		}
    384  1.17.2.3  jdolecek 
    385  1.17.2.2  jdolecek 	}
    386  1.17.2.2  jdolecek 
    387  1.17.2.2  jdolecek out:
    388  1.17.2.2  jdolecek 	/*
    389  1.17.2.2  jdolecek 	 * check to see if we need to generate some free pages waking
    390  1.17.2.2  jdolecek 	 * the pagedaemon.
    391  1.17.2.2  jdolecek 	 */
    392  1.17.2.2  jdolecek 
    393  1.17.2.2  jdolecek 	UVM_KICK_PDAEMON();
    394  1.17.2.2  jdolecek 	uvm_unlock_fpageq(s);
    395  1.17.2.2  jdolecek 	if (error) {
    396  1.17.2.2  jdolecek 		if (waitok) {
    397  1.17.2.2  jdolecek 			/* XXX perhaps some time limitation? */
    398  1.17.2.2  jdolecek #ifdef DEBUG
    399  1.17.2.2  jdolecek 			printf("pglistalloc waiting\n");
    400  1.17.2.2  jdolecek #endif
    401  1.17.2.2  jdolecek 			uvm_wait("pglalloc");
    402  1.17.2.2  jdolecek 			goto again;
    403  1.17.2.2  jdolecek 		} else
    404  1.17.2.2  jdolecek 			uvm_pglistfree(rlist);
    405  1.17.2.2  jdolecek 	}
    406  1.17.2.3  jdolecek #ifdef PGALLOC_VERBOSE
    407  1.17.2.3  jdolecek 	if (!error)
    408  1.17.2.3  jdolecek 		printf("pgalloc: %lx..%lx\n",
    409  1.17.2.3  jdolecek 		       TAILQ_FIRST(rlist)->phys_addr,
    410  1.17.2.3  jdolecek 		       TAILQ_LAST(rlist, pglist)->phys_addr);
    411  1.17.2.3  jdolecek #endif
    412  1.17.2.2  jdolecek 	return (error);
    413  1.17.2.2  jdolecek }
    414  1.17.2.2  jdolecek 
    415  1.17.2.2  jdolecek int
    416  1.17.2.2  jdolecek uvm_pglistalloc(size, low, high, alignment, boundary, rlist, nsegs, waitok)
    417  1.17.2.2  jdolecek 	psize_t size;
    418  1.17.2.2  jdolecek 	paddr_t low, high, alignment, boundary;
    419  1.17.2.2  jdolecek 	struct pglist *rlist;
    420  1.17.2.2  jdolecek 	int nsegs, waitok;
    421  1.17.2.2  jdolecek {
    422  1.17.2.3  jdolecek 	int num, res;
    423  1.17.2.2  jdolecek 
    424  1.17.2.2  jdolecek 	KASSERT((alignment & (alignment - 1)) == 0);
    425  1.17.2.2  jdolecek 	KASSERT((boundary & (boundary - 1)) == 0);
    426  1.17.2.2  jdolecek 
    427  1.17.2.2  jdolecek 	/*
    428  1.17.2.2  jdolecek 	 * Our allocations are always page granularity, so our alignment
    429  1.17.2.2  jdolecek 	 * must be, too.
    430  1.17.2.2  jdolecek 	 */
    431  1.17.2.2  jdolecek 	if (alignment < PAGE_SIZE)
    432  1.17.2.2  jdolecek 		alignment = PAGE_SIZE;
    433  1.17.2.3  jdolecek 	if (boundary != 0 && boundary < size)
    434  1.17.2.3  jdolecek 		return (EINVAL);
    435  1.17.2.3  jdolecek 	num = atop(round_page(size));
    436  1.17.2.2  jdolecek 	low = roundup(low, alignment);
    437  1.17.2.2  jdolecek 
    438  1.17.2.2  jdolecek 	TAILQ_INIT(rlist);
    439  1.17.2.2  jdolecek 
    440  1.17.2.3  jdolecek 	if ((nsegs < size >> PAGE_SHIFT) || (alignment != PAGE_SIZE) ||
    441  1.17.2.3  jdolecek 	    (boundary != 0))
    442  1.17.2.3  jdolecek 		res = uvm_pglistalloc_contig(num, low, high, alignment,
    443  1.17.2.2  jdolecek 					     boundary, rlist);
    444  1.17.2.2  jdolecek 	else
    445  1.17.2.3  jdolecek 		res = uvm_pglistalloc_simple(num, low, high, rlist, waitok);
    446  1.17.2.2  jdolecek 
    447  1.17.2.2  jdolecek 	return (res);
    448       1.1       mrg }
    449       1.1       mrg 
    450       1.1       mrg /*
    451       1.1       mrg  * uvm_pglistfree: free a list of pages
    452       1.1       mrg  *
    453       1.1       mrg  * => pages should already be unmapped
    454       1.1       mrg  */
    455       1.1       mrg 
    456       1.3       mrg void
    457       1.3       mrg uvm_pglistfree(list)
    458       1.3       mrg 	struct pglist *list;
    459       1.1       mrg {
    460  1.17.2.1   thorpej 	struct vm_page *pg;
    461       1.3       mrg 	int s;
    462       1.1       mrg 
    463       1.3       mrg 	/*
    464  1.17.2.1   thorpej 	 * Lock the free list and free each page.
    465       1.3       mrg 	 */
    466       1.1       mrg 
    467  1.17.2.1   thorpej 	s = uvm_lock_fpageq();
    468  1.17.2.1   thorpej 	while ((pg = TAILQ_FIRST(list)) != NULL) {
    469  1.17.2.1   thorpej 		KASSERT((pg->pqflags & (PQ_ACTIVE|PQ_INACTIVE)) == 0);
    470  1.17.2.1   thorpej 		TAILQ_REMOVE(list, pg, pageq);
    471  1.17.2.1   thorpej 		pg->pqflags = PQ_FREE;
    472  1.17.2.1   thorpej 		TAILQ_INSERT_TAIL(&uvm.page_free[uvm_page_lookup_freelist(pg)].
    473  1.17.2.1   thorpej 		    pgfl_buckets[VM_PGCOLOR_BUCKET(pg)].
    474  1.17.2.1   thorpej 		    pgfl_queues[PGFL_UNKNOWN], pg, pageq);
    475       1.3       mrg 		uvmexp.free++;
    476       1.9   thorpej 		if (uvmexp.zeropages < UVM_PAGEZERO_TARGET)
    477       1.9   thorpej 			uvm.page_idle_zero = vm_page_zero_enable;
    478       1.3       mrg 		STAT_DECR(uvm_pglistalloc_npages);
    479       1.3       mrg 	}
    480       1.7   thorpej 	uvm_unlock_fpageq(s);
    481       1.1       mrg }
    482