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