Home | History | Annotate | Line # | Download | only in uvm
uvm_pglist.c revision 1.17.2.2
      1  1.17.2.2  jdolecek /*	$NetBSD: uvm_pglist.c,v 1.17.2.2 2002/06/23 17:52:19 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.2  jdolecek __KERNEL_RCSID(0, "$NetBSD: uvm_pglist.c,v 1.17.2.2 2002/06/23 17:52:19 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.2  jdolecek static int uvm_pglistalloc_contig(psize_t, paddr_t, paddr_t, paddr_t, paddr_t,
     89  1.17.2.2  jdolecek 				  struct pglist *);
     90  1.17.2.2  jdolecek static int uvm_pglistalloc_simple(psize_t, paddr_t, paddr_t,
     91  1.17.2.2  jdolecek 				  struct pglist *, int);
     92  1.17.2.2  jdolecek 
     93  1.17.2.2  jdolecek static void
     94  1.17.2.2  jdolecek uvm_pglist_add(pg, rlist)
     95  1.17.2.2  jdolecek 	struct vm_page *pg;
     96       1.3       mrg 	struct pglist *rlist;
     97       1.1       mrg {
     98  1.17.2.2  jdolecek 	int free_list, color, pgflidx;
     99       1.1       mrg #ifdef DEBUG
    100      1.16       chs 	struct vm_page *tp;
    101       1.1       mrg #endif
    102       1.1       mrg 
    103  1.17.2.2  jdolecek #if PGFL_NQUEUES != 2
    104  1.17.2.2  jdolecek #error uvm_pglistalloc needs to be updated
    105  1.17.2.2  jdolecek #endif
    106      1.15       chs 
    107  1.17.2.2  jdolecek 	free_list = uvm_page_lookup_freelist(pg);
    108  1.17.2.2  jdolecek 	color = VM_PGCOLOR_BUCKET(pg);
    109  1.17.2.2  jdolecek 	pgflidx = (pg->flags & PG_ZERO) ? PGFL_ZEROS : PGFL_UNKNOWN;
    110  1.17.2.2  jdolecek #ifdef DEBUG
    111  1.17.2.2  jdolecek 	for (tp = TAILQ_FIRST(&uvm.page_free[
    112  1.17.2.2  jdolecek 		free_list].pgfl_buckets[color].pgfl_queues[pgflidx]);
    113  1.17.2.2  jdolecek 	     tp != NULL;
    114  1.17.2.2  jdolecek 	     tp = TAILQ_NEXT(tp, pageq)) {
    115  1.17.2.2  jdolecek 		if (tp == pg)
    116  1.17.2.2  jdolecek 			break;
    117  1.17.2.2  jdolecek 	}
    118  1.17.2.2  jdolecek 	if (tp == NULL)
    119  1.17.2.2  jdolecek 		panic("uvm_pglistalloc: page not on freelist");
    120  1.17.2.2  jdolecek #endif
    121  1.17.2.2  jdolecek 	TAILQ_REMOVE(&uvm.page_free[free_list].pgfl_buckets[
    122  1.17.2.2  jdolecek 			color].pgfl_queues[pgflidx], pg, pageq);
    123  1.17.2.2  jdolecek 	uvmexp.free--;
    124  1.17.2.2  jdolecek 	if (pg->flags & PG_ZERO)
    125  1.17.2.2  jdolecek 		uvmexp.zeropages--;
    126  1.17.2.2  jdolecek 	pg->flags = PG_CLEAN;
    127  1.17.2.2  jdolecek 	pg->pqflags = 0;
    128  1.17.2.2  jdolecek 	pg->uobject = NULL;
    129  1.17.2.2  jdolecek 	pg->uanon = NULL;
    130  1.17.2.2  jdolecek 	TAILQ_INSERT_TAIL(rlist, pg, pageq);
    131  1.17.2.2  jdolecek 	STAT_INCR(uvm_pglistalloc_npages);
    132  1.17.2.2  jdolecek }
    133  1.17.2.2  jdolecek 
    134  1.17.2.2  jdolecek static int
    135  1.17.2.2  jdolecek uvm_pglistalloc_contig(size, low, high, alignment, boundary, rlist)
    136  1.17.2.2  jdolecek 	psize_t size;
    137  1.17.2.2  jdolecek 	paddr_t low, high, alignment, boundary;
    138  1.17.2.2  jdolecek 	struct pglist *rlist;
    139  1.17.2.2  jdolecek {
    140  1.17.2.2  jdolecek 	paddr_t try, idxpa, lastidxpa;
    141  1.17.2.2  jdolecek 	int psi;
    142  1.17.2.2  jdolecek 	struct vm_page *pgs;
    143  1.17.2.2  jdolecek 	int s, tryidx, idx, end, error;
    144  1.17.2.2  jdolecek 	u_long pagemask;
    145  1.17.2.1   thorpej 
    146       1.3       mrg 	if (boundary != 0 && boundary < size)
    147       1.3       mrg 		return (EINVAL);
    148       1.3       mrg 	pagemask = ~(boundary - 1);
    149       1.1       mrg 
    150       1.3       mrg 	/* Default to "lose". */
    151       1.3       mrg 	error = ENOMEM;
    152       1.3       mrg 
    153       1.3       mrg 	/*
    154       1.3       mrg 	 * Block all memory allocation and lock the free list.
    155       1.3       mrg 	 */
    156  1.17.2.1   thorpej 
    157      1.12       chs 	s = uvm_lock_fpageq();
    158       1.3       mrg 
    159       1.3       mrg 	/* Are there even any free pages? */
    160      1.12       chs 	if (uvmexp.free <= (uvmexp.reserve_pagedaemon + uvmexp.reserve_kernel))
    161       1.3       mrg 		goto out;
    162       1.3       mrg 
    163  1.17.2.2  jdolecek 	for (try = low;; try += alignment) {
    164       1.3       mrg 		if (try + size > high) {
    165      1.12       chs 
    166       1.3       mrg 			/*
    167       1.3       mrg 			 * We've run past the allowable range.
    168       1.3       mrg 			 */
    169      1.12       chs 
    170       1.3       mrg 			goto out;
    171       1.3       mrg 		}
    172       1.3       mrg 
    173       1.3       mrg 		/*
    174       1.3       mrg 		 * Make sure this is a managed physical page.
    175       1.3       mrg 		 */
    176       1.3       mrg 
    177       1.3       mrg 		if ((psi = vm_physseg_find(atop(try), &idx)) == -1)
    178       1.3       mrg 			continue; /* managed? */
    179       1.3       mrg 		if (vm_physseg_find(atop(try + size), NULL) != psi)
    180       1.3       mrg 			continue; /* end must be in this segment */
    181       1.3       mrg 		tryidx = idx;
    182       1.3       mrg 		end = idx + (size / PAGE_SIZE);
    183       1.3       mrg 		pgs = vm_physmem[psi].pgs;
    184       1.3       mrg 
    185       1.3       mrg 		/*
    186       1.3       mrg 		 * Found a suitable starting page.  See of the range is free.
    187       1.3       mrg 		 */
    188      1.12       chs 
    189       1.3       mrg 		for (; idx < end; idx++) {
    190       1.3       mrg 			if (VM_PAGE_IS_FREE(&pgs[idx]) == 0) {
    191       1.3       mrg 				break;
    192       1.3       mrg 			}
    193       1.3       mrg 			idxpa = VM_PAGE_TO_PHYS(&pgs[idx]);
    194       1.3       mrg 			if (idx > tryidx) {
    195       1.3       mrg 				lastidxpa = VM_PAGE_TO_PHYS(&pgs[idx - 1]);
    196      1.12       chs 				if ((lastidxpa + PAGE_SIZE) != idxpa) {
    197       1.3       mrg 
    198       1.3       mrg 					/*
    199       1.3       mrg 					 * Region not contiguous.
    200       1.3       mrg 					 */
    201      1.12       chs 
    202       1.3       mrg 					break;
    203       1.3       mrg 				}
    204       1.3       mrg 				if (boundary != 0 &&
    205       1.3       mrg 				    ((lastidxpa ^ idxpa) & pagemask) != 0) {
    206      1.12       chs 
    207       1.3       mrg 					/*
    208       1.3       mrg 					 * Region crosses boundary.
    209       1.3       mrg 					 */
    210      1.12       chs 
    211       1.3       mrg 					break;
    212       1.3       mrg 				}
    213       1.3       mrg 			}
    214       1.3       mrg 		}
    215       1.3       mrg 		if (idx == end) {
    216       1.3       mrg 			break;
    217       1.3       mrg 		}
    218       1.1       mrg 	}
    219       1.1       mrg 
    220       1.3       mrg 	/*
    221       1.3       mrg 	 * we have a chunk of memory that conforms to the requested constraints.
    222       1.3       mrg 	 */
    223       1.3       mrg 	idx = tryidx;
    224       1.3       mrg 	while (idx < end) {
    225  1.17.2.2  jdolecek 		uvm_pglist_add(&pgs[idx++], rlist);
    226       1.3       mrg 	}
    227       1.3       mrg 	error = 0;
    228       1.1       mrg 
    229       1.1       mrg out:
    230       1.3       mrg 	/*
    231       1.3       mrg 	 * check to see if we need to generate some free pages waking
    232       1.3       mrg 	 * the pagedaemon.
    233       1.3       mrg 	 */
    234      1.15       chs 
    235      1.17   thorpej 	UVM_KICK_PDAEMON();
    236      1.12       chs 	uvm_unlock_fpageq(s);
    237       1.3       mrg 	return (error);
    238  1.17.2.2  jdolecek }
    239  1.17.2.2  jdolecek 
    240  1.17.2.2  jdolecek static int
    241  1.17.2.2  jdolecek uvm_pglistalloc_simple(size, low, high, rlist, waitok)
    242  1.17.2.2  jdolecek 	psize_t size;
    243  1.17.2.2  jdolecek 	paddr_t low, high;
    244  1.17.2.2  jdolecek 	struct pglist *rlist;
    245  1.17.2.2  jdolecek 	int waitok;
    246  1.17.2.2  jdolecek {
    247  1.17.2.2  jdolecek 	psize_t try;
    248  1.17.2.2  jdolecek 	int psi;
    249  1.17.2.2  jdolecek 	struct vm_page *pg;
    250  1.17.2.2  jdolecek 	int s, todo, idx, error;
    251  1.17.2.2  jdolecek 
    252  1.17.2.2  jdolecek 	/* Default to "lose". */
    253  1.17.2.2  jdolecek 	error = ENOMEM;
    254  1.17.2.2  jdolecek 
    255  1.17.2.2  jdolecek 	todo = size / PAGE_SIZE;
    256  1.17.2.2  jdolecek 
    257  1.17.2.2  jdolecek again:
    258  1.17.2.2  jdolecek 	/*
    259  1.17.2.2  jdolecek 	 * Block all memory allocation and lock the free list.
    260  1.17.2.2  jdolecek 	 */
    261  1.17.2.2  jdolecek 
    262  1.17.2.2  jdolecek 	s = uvm_lock_fpageq();
    263  1.17.2.2  jdolecek 
    264  1.17.2.2  jdolecek 	/* Are there even any free pages? */
    265  1.17.2.2  jdolecek 	if (uvmexp.free <= (uvmexp.reserve_pagedaemon + uvmexp.reserve_kernel))
    266  1.17.2.2  jdolecek 		goto out;
    267  1.17.2.2  jdolecek 
    268  1.17.2.2  jdolecek 	for (try = low; try < high; try += PAGE_SIZE) {
    269  1.17.2.2  jdolecek 
    270  1.17.2.2  jdolecek 		/*
    271  1.17.2.2  jdolecek 		 * Make sure this is a managed physical page.
    272  1.17.2.2  jdolecek 		 */
    273  1.17.2.2  jdolecek 
    274  1.17.2.2  jdolecek 		if ((psi = vm_physseg_find(atop(try), &idx)) == -1)
    275  1.17.2.2  jdolecek 			continue; /* managed? */
    276  1.17.2.2  jdolecek 		pg = &vm_physmem[psi].pgs[idx];
    277  1.17.2.2  jdolecek 		if (VM_PAGE_IS_FREE(pg) == 0)
    278  1.17.2.2  jdolecek 			continue;
    279  1.17.2.2  jdolecek 
    280  1.17.2.2  jdolecek 		uvm_pglist_add(pg, rlist);
    281  1.17.2.2  jdolecek 		if (--todo == 0) {
    282  1.17.2.2  jdolecek 			error = 0;
    283  1.17.2.2  jdolecek 			goto out;
    284  1.17.2.2  jdolecek 		}
    285  1.17.2.2  jdolecek 	}
    286  1.17.2.2  jdolecek 
    287  1.17.2.2  jdolecek out:
    288  1.17.2.2  jdolecek 	/*
    289  1.17.2.2  jdolecek 	 * check to see if we need to generate some free pages waking
    290  1.17.2.2  jdolecek 	 * the pagedaemon.
    291  1.17.2.2  jdolecek 	 */
    292  1.17.2.2  jdolecek 
    293  1.17.2.2  jdolecek 	UVM_KICK_PDAEMON();
    294  1.17.2.2  jdolecek 	uvm_unlock_fpageq(s);
    295  1.17.2.2  jdolecek 	if (error) {
    296  1.17.2.2  jdolecek 		if (waitok) {
    297  1.17.2.2  jdolecek 			/* XXX perhaps some time limitation? */
    298  1.17.2.2  jdolecek #ifdef DEBUG
    299  1.17.2.2  jdolecek 			printf("pglistalloc waiting\n");
    300  1.17.2.2  jdolecek #endif
    301  1.17.2.2  jdolecek 			uvm_wait("pglalloc");
    302  1.17.2.2  jdolecek 			goto again;
    303  1.17.2.2  jdolecek 		} else
    304  1.17.2.2  jdolecek 			uvm_pglistfree(rlist);
    305  1.17.2.2  jdolecek 	}
    306  1.17.2.2  jdolecek 	return (error);
    307  1.17.2.2  jdolecek }
    308  1.17.2.2  jdolecek 
    309  1.17.2.2  jdolecek int
    310  1.17.2.2  jdolecek uvm_pglistalloc(size, low, high, alignment, boundary, rlist, nsegs, waitok)
    311  1.17.2.2  jdolecek 	psize_t size;
    312  1.17.2.2  jdolecek 	paddr_t low, high, alignment, boundary;
    313  1.17.2.2  jdolecek 	struct pglist *rlist;
    314  1.17.2.2  jdolecek 	int nsegs, waitok;
    315  1.17.2.2  jdolecek {
    316  1.17.2.2  jdolecek 	int res;
    317  1.17.2.2  jdolecek 
    318  1.17.2.2  jdolecek 	KASSERT((alignment & (alignment - 1)) == 0);
    319  1.17.2.2  jdolecek 	KASSERT((boundary & (boundary - 1)) == 0);
    320  1.17.2.2  jdolecek 
    321  1.17.2.2  jdolecek 	/*
    322  1.17.2.2  jdolecek 	 * Our allocations are always page granularity, so our alignment
    323  1.17.2.2  jdolecek 	 * must be, too.
    324  1.17.2.2  jdolecek 	 */
    325  1.17.2.2  jdolecek 	if (alignment < PAGE_SIZE)
    326  1.17.2.2  jdolecek 		alignment = PAGE_SIZE;
    327  1.17.2.2  jdolecek 	size = round_page(size);
    328  1.17.2.2  jdolecek 	low = roundup(low, alignment);
    329  1.17.2.2  jdolecek 
    330  1.17.2.2  jdolecek 	TAILQ_INIT(rlist);
    331  1.17.2.2  jdolecek 
    332  1.17.2.2  jdolecek 	if ((nsegs < size / PAGE_SIZE) || (alignment != PAGE_SIZE)
    333  1.17.2.2  jdolecek 	    || (boundary != 0))
    334  1.17.2.2  jdolecek 		res = uvm_pglistalloc_contig(size, low, high, alignment,
    335  1.17.2.2  jdolecek 					     boundary, rlist);
    336  1.17.2.2  jdolecek 	else
    337  1.17.2.2  jdolecek 		res = uvm_pglistalloc_simple(size, low, high, rlist, waitok);
    338  1.17.2.2  jdolecek 
    339  1.17.2.2  jdolecek 	return (res);
    340       1.1       mrg }
    341       1.1       mrg 
    342       1.1       mrg /*
    343       1.1       mrg  * uvm_pglistfree: free a list of pages
    344       1.1       mrg  *
    345       1.1       mrg  * => pages should already be unmapped
    346       1.1       mrg  */
    347       1.1       mrg 
    348       1.3       mrg void
    349       1.3       mrg uvm_pglistfree(list)
    350       1.3       mrg 	struct pglist *list;
    351       1.1       mrg {
    352  1.17.2.1   thorpej 	struct vm_page *pg;
    353       1.3       mrg 	int s;
    354       1.1       mrg 
    355       1.3       mrg 	/*
    356  1.17.2.1   thorpej 	 * Lock the free list and free each page.
    357       1.3       mrg 	 */
    358       1.1       mrg 
    359  1.17.2.1   thorpej 	s = uvm_lock_fpageq();
    360  1.17.2.1   thorpej 	while ((pg = TAILQ_FIRST(list)) != NULL) {
    361  1.17.2.1   thorpej 		KASSERT((pg->pqflags & (PQ_ACTIVE|PQ_INACTIVE)) == 0);
    362  1.17.2.1   thorpej 		TAILQ_REMOVE(list, pg, pageq);
    363  1.17.2.1   thorpej 		pg->pqflags = PQ_FREE;
    364  1.17.2.1   thorpej 		TAILQ_INSERT_TAIL(&uvm.page_free[uvm_page_lookup_freelist(pg)].
    365  1.17.2.1   thorpej 		    pgfl_buckets[VM_PGCOLOR_BUCKET(pg)].
    366  1.17.2.1   thorpej 		    pgfl_queues[PGFL_UNKNOWN], pg, pageq);
    367       1.3       mrg 		uvmexp.free++;
    368       1.9   thorpej 		if (uvmexp.zeropages < UVM_PAGEZERO_TARGET)
    369       1.9   thorpej 			uvm.page_idle_zero = vm_page_zero_enable;
    370       1.3       mrg 		STAT_DECR(uvm_pglistalloc_npages);
    371       1.3       mrg 	}
    372       1.7   thorpej 	uvm_unlock_fpageq(s);
    373       1.1       mrg }
    374