Home | History | Annotate | Line # | Download | only in uvm
uvm_pglist.c revision 1.10
      1 /*	$NetBSD: uvm_pglist.c,v 1.10 2000/05/20 19:54:01 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *      This product includes software developed by the NetBSD
     22  *      Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * uvm_pglist.c: pglist functions
     42  */
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/malloc.h>
     47 #include <sys/proc.h>
     48 
     49 #include <vm/vm.h>
     50 
     51 #include <uvm/uvm.h>
     52 
     53 #ifdef VM_PAGE_ALLOC_MEMORY_STATS
     54 #define	STAT_INCR(v)	(v)++
     55 #define	STAT_DECR(v)	do { \
     56 		if ((v) == 0) \
     57 			printf("%s:%d -- Already 0!\n", __FILE__, __LINE__); \
     58 		else \
     59 			(v)--; \
     60 	} while (0)
     61 u_long	uvm_pglistalloc_npages;
     62 #else
     63 #define	STAT_INCR(v)
     64 #define	STAT_DECR(v)
     65 #endif
     66 
     67 /*
     68  * uvm_pglistalloc: allocate a list of pages
     69  *
     70  * => allocated pages are placed at the tail of rlist.  rlist is
     71  *    assumed to be properly initialized by caller.
     72  * => returns 0 on success or errno on failure
     73  * => XXX: implementation allocates only a single segment, also
     74  *	might be able to better advantage of vm_physeg[].
     75  * => doesn't take into account clean non-busy pages on inactive list
     76  *	that could be used(?)
     77  * => params:
     78  *	size		the size of the allocation, rounded to page size.
     79  *	low		the low address of the allowed allocation range.
     80  *	high		the high address of the allowed allocation range.
     81  *	alignment	memory must be aligned to this power-of-two boundary.
     82  *	boundary	no segment in the allocation may cross this
     83  *			power-of-two boundary (relative to zero).
     84  */
     85 
     86 int
     87 uvm_pglistalloc(size, low, high, alignment, boundary, rlist, nsegs, waitok)
     88 	psize_t size;
     89 	paddr_t low, high, alignment, boundary;
     90 	struct pglist *rlist;
     91 	int nsegs, waitok;
     92 {
     93 	paddr_t try, idxpa, lastidxpa;
     94 	int psi;
     95 	struct vm_page *pgs;
     96 	int s, tryidx, idx, pgflidx, end, error, free_list;
     97 	vm_page_t m;
     98 	u_long pagemask;
     99 #ifdef DEBUG
    100 	vm_page_t tp;
    101 #endif
    102 
    103 #ifdef DIAGNOSTIC
    104 	if ((alignment & (alignment - 1)) != 0)
    105 		panic("uvm_pglistalloc: alignment must be power of 2");
    106 
    107 	if ((boundary & (boundary - 1)) != 0)
    108 		panic("uvm_pglistalloc: boundary must be power of 2");
    109 #endif
    110 
    111 	/*
    112 	 * Our allocations are always page granularity, so our alignment
    113 	 * must be, too.
    114 	 */
    115 	if (alignment < PAGE_SIZE)
    116 		alignment = PAGE_SIZE;
    117 
    118 	size = round_page(size);
    119 	try = roundup(low, alignment);
    120 
    121 	if (boundary != 0 && boundary < size)
    122 		return (EINVAL);
    123 
    124 	pagemask = ~(boundary - 1);
    125 
    126 	/* Default to "lose". */
    127 	error = ENOMEM;
    128 
    129 	/*
    130 	 * Block all memory allocation and lock the free list.
    131 	 */
    132 	s = uvm_lock_fpageq();		/* lock free page queue */
    133 
    134 	/* Are there even any free pages? */
    135 	if (uvmexp.free <= (uvmexp.reserve_pagedaemon +
    136 	    uvmexp.reserve_kernel))
    137 		goto out;
    138 
    139 	for (;; try += alignment) {
    140 		if (try + size > high) {
    141 			/*
    142 			 * We've run past the allowable range.
    143 			 */
    144 			goto out;
    145 		}
    146 
    147 		/*
    148 		 * Make sure this is a managed physical page.
    149 		 */
    150 
    151 		if ((psi = vm_physseg_find(atop(try), &idx)) == -1)
    152 			continue; /* managed? */
    153 		if (vm_physseg_find(atop(try + size), NULL) != psi)
    154 			continue; /* end must be in this segment */
    155 
    156 		tryidx = idx;
    157 		end = idx + (size / PAGE_SIZE);
    158 		pgs = vm_physmem[psi].pgs;
    159 
    160 		/*
    161 		 * Found a suitable starting page.  See of the range is free.
    162 		 */
    163 		for (; idx < end; idx++) {
    164 			if (VM_PAGE_IS_FREE(&pgs[idx]) == 0) {
    165 				/*
    166 				 * Page not available.
    167 				 */
    168 				break;
    169 			}
    170 
    171 			idxpa = VM_PAGE_TO_PHYS(&pgs[idx]);
    172 
    173 			if (idx > tryidx) {
    174 				lastidxpa = VM_PAGE_TO_PHYS(&pgs[idx - 1]);
    175 
    176 				if ((lastidxpa + PAGE_SIZE) != idxpa) {
    177 					/*
    178 					 * Region not contiguous.
    179 					 */
    180 					break;
    181 				}
    182 				if (boundary != 0 &&
    183 				    ((lastidxpa ^ idxpa) & pagemask) != 0) {
    184 					/*
    185 					 * Region crosses boundary.
    186 					 */
    187 					break;
    188 				}
    189 			}
    190 		}
    191 
    192 		if (idx == end) {
    193 			/*
    194 			 * Woo hoo!  Found one.
    195 			 */
    196 			break;
    197 		}
    198 	}
    199 
    200 #if PGFL_NQUEUES != 2
    201 #error uvm_pglistalloc needs to be updated
    202 #endif
    203 
    204 	/*
    205 	 * we have a chunk of memory that conforms to the requested constraints.
    206 	 */
    207 	idx = tryidx;
    208 	while (idx < end) {
    209 		m = &pgs[idx];
    210 		free_list = uvm_page_lookup_freelist(m);
    211 		pgflidx = (m->flags & PG_ZERO) ? PGFL_ZEROS : PGFL_UNKNOWN;
    212 #ifdef DEBUG
    213 		for (tp = TAILQ_FIRST(&uvm.page_free[
    214 		  free_list].pgfl_queues[pgflidx]);
    215 		     tp != NULL;
    216 		     tp = TAILQ_NEXT(tp, pageq)) {
    217 			if (tp == m)
    218 				break;
    219 		}
    220 		if (tp == NULL)
    221 			panic("uvm_pglistalloc: page not on freelist");
    222 #endif
    223 		TAILQ_REMOVE(&uvm.page_free[free_list].pgfl_queues[pgflidx],
    224 		    m, pageq);
    225 		uvmexp.free--;
    226 		if (m->flags & PG_ZERO)
    227 			uvmexp.zeropages--;
    228 		m->flags = PG_CLEAN;
    229 		m->pqflags = 0;
    230 		m->uobject = NULL;
    231 		m->uanon = NULL;
    232 		m->wire_count = 0;
    233 		m->loan_count = 0;
    234 		TAILQ_INSERT_TAIL(rlist, m, pageq);
    235 		idx++;
    236 		STAT_INCR(uvm_pglistalloc_npages);
    237 	}
    238 	error = 0;
    239 
    240 out:
    241 	uvm_unlock_fpageq(s);
    242 
    243 	/*
    244 	 * check to see if we need to generate some free pages waking
    245 	 * the pagedaemon.
    246 	 * XXX: we read uvm.free without locking
    247 	 */
    248 
    249 	if (uvmexp.free < uvmexp.freemin ||
    250 	    (uvmexp.free < uvmexp.freetarg &&
    251 	    uvmexp.inactive < uvmexp.inactarg))
    252 		wakeup(&uvm.pagedaemon);
    253 
    254 	return (error);
    255 }
    256 
    257 /*
    258  * uvm_pglistfree: free a list of pages
    259  *
    260  * => pages should already be unmapped
    261  */
    262 
    263 void
    264 uvm_pglistfree(list)
    265 	struct pglist *list;
    266 {
    267 	vm_page_t m;
    268 	int s;
    269 
    270 	/*
    271 	 * Block all memory allocation and lock the free list.
    272 	 */
    273 	s = uvm_lock_fpageq();
    274 
    275 	while ((m = list->tqh_first) != NULL) {
    276 #ifdef DIAGNOSTIC
    277 		if (m->pqflags & (PQ_ACTIVE|PQ_INACTIVE))
    278 			panic("uvm_pglistfree: active/inactive page!");
    279 #endif
    280 		TAILQ_REMOVE(list, m, pageq);
    281 		m->pqflags = PQ_FREE;
    282 		TAILQ_INSERT_TAIL(&uvm.page_free[
    283 		    uvm_page_lookup_freelist(m)].pgfl_queues[PGFL_UNKNOWN],
    284 		    m, pageq);
    285 		uvmexp.free++;
    286 		if (uvmexp.zeropages < UVM_PAGEZERO_TARGET)
    287 			uvm.page_idle_zero = vm_page_zero_enable;
    288 		STAT_DECR(uvm_pglistalloc_npages);
    289 	}
    290 
    291 	uvm_unlock_fpageq(s);
    292 }
    293